the program bellow has a problem on \" the search value method \" when you run i
ID: 3819766 • Letter: T
Question
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<size; i++){
//Searching for value
if(found == false && array[i] == value)
{
//Updating position
index = i;
found = true;
}
}
//Return index where element is found
return index;
}
}
Explanation / Answer
Hi your program is working fine it does not have any problem please ensure you typed capital 'Y' and capital 'N'
and here is your output of your program:
Enter a number: 10
Enter a number: 20
Enter a number: 30
Enter a number: 40
Enter a number: -3
As a total 4 numbers were read.
Smallest Number: 10
Largest Number: 40
Median: 25.00
Average: 25.00
Enter a value to search: 10
Element found at index: 0
Do you want to search more (Y/N): Y
Enter a value to search: 40
Element found at index: 3
Do you want to search more (Y/N): Y
Enter a value to search: 22
Element not found in the list...
Do you want to search more (Y/N): Y
Enter a value to search: 50
Element not found in the list...
Do you want to search more (Y/N): Y
Enter a value to search: 60
Element not found in the list...
Do you want to search more (Y/N): Y
Enter a value to search: 70
Element not found in the list...
Do you want to search more (Y/N): Y
Enter a value to search: 80
Element not found in the list...
Do you want to search more (Y/N): Y
Enter a value to search: 10
Element found at index: 0
Do you want to search more (Y/N): N
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.