Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

The program bellow works just fine on java, the problem is, if a user eneters a

ID: 3818846 • Letter: T

Question

The program bellow works just fine on java, the problem is, if a user eneters a number which is not an integer( 3.1, 5.66) I end up getting a run time error. I need some one to help me fix this problem so that if a user eneters a number that is not an interger like 3.4, 6.2....a message should be displayed on the screen which says "you need to eneter an integer please". instead of a run time error. this message should be displayed until an integer is entered and then should continue.

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: ");
           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        {
           //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        {
           //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

Please find updated code. It'll work as you need

package com.code.programs.chegg;

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;

   }

}

Sample Output

Enter a number: 3

Enter a number: 7

Enter a number: 2

Enter a number: 2.3

You need to enter an integer please

Enter a number: 6.7

You need to enter an integer please

Enter a number: 8

Enter a number: -1

As a total 4 numbers were read.

Smallest Number: 3

Largest Number: 8

Median: 4.50

Average: 5.00

Enter a value to search: 7

Element found at index: 1

Do you want to search more (Y/N): n