-2

I'm trying to calculate the average of N non-negative integers. I ask the user to input how many numbers they want to calculate the average of. Then ask them to input the numbers one by one. I'm also using a try and catch inside a do statement so I can re-prompt user again in case they input the wrong value.

How can I re-prompt user if any of the entered N numbers is not a number (int).? What I have re-prompts the user to restart again. I'd appreciate some directions!

Edit: I used the same do while loop to catch if input is non-numeric. What I get now is infinite number of prints that prints error and re prompt user to input. I have to stop program from running.

Edit: The problem was due to the scanner not getting cleared. Fixed by adding a second scanner inside nested do-while loop.

public class Average {

    public static void main(String[] args) {

        boolean flag = false;

        do {
        try {
            System.out.println("Enter the number of integers you want to "
                               + "calculate the average of: ");

            Scanner input = new Scanner(System.in);
            int value = input.nextInt();
            int[] numbers = new int[value];
            int sum = 0; 

            if( value == 0 ) {
                throw new ArithmeticException();
            }
            boolean flag2 = false;
            do{
                try{

                    Scanner sc = new Scanner(System.in);
                    for( int i = 0; i < numbers.length; i++) {
                    System.out.println("Enter the " + (i+1) + " number: ");
                    numbers[i] = sc.nextInt();
                    sum += numbers[i];
                    flag2 = true;

                    }    
                } catch ( InputMismatchException e ) {
                    System.err.println("Cannot calculate average of non-numeric values.");
                } catch ( NumberFormatException e) {
                    System.out.println("Cannot calculate average of non-numeric values.!!");
                }
            } while (!flag2);


            double average = (double) sum / numbers.length;
            System.out.println("The numbers you have entered are:                " + Arrays.toString(numbers));
            System.out.println("The sum of the numbers is:                       " + sum);
            System.out.println("The number to divide by is:                      " + numbers.length);
            System.out.println("The average of the numbers you have entered is:  " + average);
            flag = true;

            } catch (InputMismatchException e) {
                System.err.println("Input cannot be non-numeric values");


            } catch (ArithmeticException e) {
                System.err.println("Input can only be positive integers");


            } catch (NegativeArraySizeException e) {
                System.err.println("Input can only be positive integers");
            }
        } while (!flag);
    }  
}
ekad
  • 14,436
  • 26
  • 44
  • 46
wa7d
  • 129
  • 1
  • 2
  • 12
  • Possible duplicate of [How to loop user input until an integer is inputted?](http://stackoverflow.com/questions/19130217/how-to-loop-user-input-until-an-integer-is-inputted) – Andrew Li Sep 20 '16 at 01:05
  • @AndrewL. not really. I'm able to handle user input if it's not integer but what I want to know is how to not have the whole thing restart when user input non-integer inside the loop. It handles non-integers but it doesn't re-prompt them to the point they were, instead it restarts – wa7d Sep 20 '16 at 01:08
  • you've already done it on your `do while` loop, use the same methodology on your `for` loop – Samuel Kok Sep 20 '16 at 01:16
  • @SamuelKok I tried that and user was re-prompted to enter 1st number again and again.. any tips? – wa7d Sep 20 '16 at 01:18
  • @wa7d use a while loop and increment the counter (that corresponds to the index of the array) only on successful insertion. – Samuel Kok Sep 20 '16 at 01:21
  • @SamuelKok I used the same { do while } now it prints the error many times till I shut the program. I updated my code here – wa7d Sep 20 '16 at 01:30
  • @wa7d refer to my answer for code reference – Samuel Kok Sep 20 '16 at 01:32
  • @SamuelKok I solved it by adding another scanner inside the nested do while loop. The scanner buffer wasn't getting cleared. Now it works! thanks – wa7d Sep 20 '16 at 01:46

2 Answers2

0

When user enters a non-number, the program should be throwing NumberFormatException. You would need to handle that case along with rest of the catch blocks

    } catch (NumberFormatExceptione) {
        System.err.println("Input can only be only numbers");
    }
blr
  • 908
  • 4
  • 8
0

You'll need something like this to increment the counter only when a valid input is received. Do remember to clear the scanner buffer when an incorrect input is received.

while (counter < numbers.length) {
    try {
        numbers[counter] = input.nextInt();
        counter++;
    } catch (InputMismatchException e) {
        input.nextLine(); //This is to clear the scanner which is now holding the incorrect input

    } catch (NumberFormatException e) {
        input.nextLine(); 
    }
}
System.out.println(Arrays.toString(numbers));
Samuel Kok
  • 585
  • 8
  • 16