the progaram bellow works just fine but the problem occurs on \" the reading the
ID: 3818967 • Letter: T
Question
the progaram bellow works just fine but the problem occurs on "the reading the search value" line( 76) If the user enters a search value that is not an entger (for example 3.21, 9.4,1.2....) I get a run time error instead of the usual statement "Element not found in the list". can you help me fix this so that i do not get a run time error when an integer is not entered. the problem can be fixed begining with line 77.
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
77. System.out.print(" Enter a value to search: ");
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<array.length;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<array.length;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
I have fixed the issue and working file now.
ListStats.java
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
{
//Reading search value
System.out.print(" Enter a value to search: ");
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<array.length;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<array.length;i++) {
//Searching for value
if(found == false && array[i] == value)
{
//Updating position
index = i;
found = true;
}
}
//Return index where element is found
return index;
}
}
Output:
Enter a number: 123
Enter a number: 123
Enter a number: 2
Enter a number: 1
Enter a number: 4
Enter a number: 5
Enter a number: 6
Enter a number: 8
Enter a number: -1
As a total 8 numbers were read.
Smallest Number: 123
Largest Number: 8
Median: 2.50
Average: 34.00
Enter a value to search: 3
Element not found in the list...
Do you want to search more (Y/N): y
Enter a value to search: 1
Element found at index: 3
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.