0

I am making a method in order to get how many numbers the user wants to sum together. aka if they want to sum the numbers 1 2 and 3. they would want to sum 3 numbers together. So when i ask them how many they want to sum together, I use a try - catch in order to catch if they enter a decimal place. Because you cant add together 3.5 numbers you can add 3 numbers or 4. problem is if the user enters a decimal, the program will infinite loop run everything but what is in the try statement. How can i fix this?

Here is the code for the method:

private static int requestMaxInputForFloat(Scanner scanner){
    boolean appropriateAnswer = true; // assume appropriate catch not appropriate to loop again
    int howManyInputs = 1000000; // hold value to return how many inputs. if this value we will not run.

    //request an appropriate number of inputs until appropriate answer = true;
    do
    {
        appropriateAnswer = true; //if looped again reset value to true
        try{
            System.out.print("How many decimal place numbers would you like to sum? ");
            howManyInputs = scanner.nextInt();
        }catch(Exception e){
            System.out.println("Sorry but you can only request to average a whole number set of data.\nTry Again.");
            appropriateAnswer = false;
        }//end of try-catch
        if (howManyInputs <= 0) //invalid answer
        {
            System.out.println("Sorry but "  + howManyInputs + " is equal to or below 0. Please try again.");
        }else{
            appropriateAnswer = false;
        }
    }while(!appropriateAnswer);//end of while loop

    return howManyInputs; //return the value 
}// end of getMaxInput
Evan Nudd
  • 216
  • 2
  • 10
  • add a new line after you do `scanner.nextInt()` and run again. – H-Patel Sep 11 '13 at 01:47
  • Im still getting an infinite loop. even with the new line after the nextInt(); im noticing now that it is running the Try loop and printing the print(); but its as though its skipping the .nextInt(); part. – Evan Nudd Sep 11 '13 at 01:51
  • 1
    Try `scanner.nextLine()` in the `catch` block. I think the problem is that if `nextInt()` gets an error, the scanner's "pointer" is still pointing at a bad character (like the decimal point), and if you just try `nextInt()` again, it will try to scan the same bad character over again. You have to do something to make the scanner skip over it. – ajb Sep 11 '13 at 01:53
  • OK, I'll post it as an answer... – ajb Sep 11 '13 at 01:57

1 Answers1

0

Add scanner.nextLine(); in the catch block. I think the problem is that if nextInt() gets an error, the scanner's "pointer" is still pointing at a bad character, and if you just try nextInt() again, it will try to scan the same bad character over again. You have to do something to make the scanner skip over it. Here, you want to just throw away whatever the user typed in, so nextLine(), which skips over the entire remainder of the input line, is the most appropriate.

One other thing: I'd change

if (howManyInputs <= 0) //invalid answer

to

if (appropriateAnswer && howManyInputs <= 0) //invalid answer

Otherwise, if the user types in -1, then the loop will go back and howManyInputs will still be -1; then if the user types 3.5, you'll get the exception but you'll get a second error message because howManyInputs is still -1 left over from the previous loop. You don't need to test howManyInputs if you already know there was an input error.

ajb
  • 31,309
  • 3
  • 58
  • 84