Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

the program bellow has a problem on the search value method when you run it, at the end when you are being asked

the program bellow has a problem on "the search value method " when you run it, at the end when you are being asked to enter a search value, when you type a number which is not an integer like 3.2, 2.1, 4.52...., the program say element not found and the asks you if you want to search more (y,N), but then it stops and will not run any more, which means you can not type the answer to the question.

I need some help so that I will be able to type an answer to the question and continue with the search until i type "N" . which means the program should no stop all together but contiue.

import java.util.*; class ListStats { //Scanner class object static Scanner keyboard = new Scanner(System.in); //Main method public static void main(String args[]) { //Size int maxSize = 100; //Creating an array int[] list = new int[maxSize]; int listSize, searchValue, index, i; //Reading numbers into array listSize = getNumbers(list); //Printing count System.out.printf(" As a total %d numbers were read. ", listSize); //Smallest number System.out.printf(" Smallest Number: %d ", list[0]); //Largest number System.out.printf(" Largest Number: %d ", list[listSize-1]); //Median System.out.printf(" Median: %.2f ", calculateMedian(list, listSize)); //Average System.out.printf(" Average: %.2f ", calculateAverage(list, listSize)); String ans; do { //76. Reading search value System.out.print(" Enter a value to search: "); if(!keyboard.hasNextInt()) index = -1; else { searchValue = keyboard.nextInt(); //Finding index index = search(list, listSize, searchValue); } //Not found if(index == -1) { System.out.println(" Element not found in the list... "); } //Found else { //Printing index System.out.println(" Element found at index: " + index + " "); } //Prompting user System.out.print(" Do you want to search more (Y/N): "); ans = keyboard.next(); }while(ans.equalsIgnoreCase("Y")); System.out.println(" "); } //Method that reads numbers from user public static int getNumbers(int[] list) { int i=0, val=0; //Iterate till user enters a negative value or max size is reached while(val>=0) { //Prompting for number System.out.print(" Enter a number: "); if(!keyboard.hasNextInt()) //Checking if the next input is an integer { System.out.println(" You need to enter an integer please"); //Printing the message to user keyboard.next(); //Discarding this input continue; //Discarding the next lines in the loop and taking control to next iteration } val = keyboard.nextInt(); if(val > 0) { //Placing number in array list[i] = val; //Incrementing number of numbers entered i++; //Checking for max size if(i>=100) { //Printing error message System.out.println(" Maximum Size reached... "); val = -1; } } } //Return number count return i; } //Method that calculates median public static double calculateMedian(int[] array, int len) { double median; //Even length if (len % 2 == 0) { median = ((double)array[len/2] + (double)array[len/2 - 1])/2; } //Odd length else { median = (double) array[len/2]; } return median; } //Method that calculates average of numbers public static double calculateAverage(int[] array, int size) { int i, sum=0; double avg; //Iterating over array for(i=0; i < size; i++) { //Accumulating sum sum = sum + array[i]; } //Calculating average avg = sum / (double)size; //Return average return avg; } //Method that searches for a value public static int search(int[] array, int size, int value) { int i, index = -1; boolean found = false; //Iterating over array for(i=0; i

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Sams Teach Yourself Beginning Databases In 24 Hours

Authors: Ryan Stephens, Ron Plew

1st Edition

067232492X, 978-0672324925

More Books

Students also viewed these Databases questions

Question

How are members held accountable for serving in the assigned roles?

Answered: 1 week ago