0

I'm working on a Java program that asks for numbers from the user and when they don't enter a number it's caught in an InputMismatchException exception. When the exception occurs twice, the program should end and give the user the sum of the numbers. However, I can't get the program to even run in it's current state. When I run the program it instantly terminates and doesn't even ask for any input. Once I get that figured out, I'm not even sure if what I have will work! Any guidance is appreciated.

public static void main(String[] args) {

    float input = 0; 
    float sum = 0; 
    int count = 0; 
    boolean attempt = true;
    Scanner in = new Scanner(System.in); 

    while (attempt)
    {
        try
        {
            System.out.print("Enter a number: "); 
            input = in.nextFloat(); 
            sum += input; 
        }
        catch(InputMismatchException e)
        {
            System.out.println("Not a number. Try again"); 
            count ++; 
            if (count >=2)
            {
                attempt = false; 
            }
        }
    }

    System.out.println("Sum: " + sum); 


}

}

  • I believe you have [`end of line`](http://stackoverflow.com/questions/13102045/skipping-nextline-after-use-nextint) problem, when `nextFloat()` does not consume end-of-line characters right away. – PM 77-1 Nov 29 '14 at 02:59
  • "_When I run the program it instantly terminates and doesn't even ask for any input._" I copy/pasted your code and it prompted me just fine. When I enter a non number once it instantly terminates. Is that what you are experiencing? – takendarkk Nov 29 '14 at 02:59
  • Just asking, the code is inside a class declaration, right? – Eric Platon Nov 29 '14 at 03:01
  • 1
    Just confirming what @Takendarkk says: I copied and pasted your code too, and it works just fine. Make sure you imported java.util.Scanner and java.util.InputMismatchException. – John Nov 29 '14 at 03:05

1 Answers1

2

All I did was add an in.nextLine() to the catch clause to correct the code.

in.nextLine();
System.out.println("Not a number. Try again"); 
count ++;

Your Scanner object had a hanging end of line character left over from entering a non number that was being instantly picked up by in.nextFloat() on the 2nd iteration.

Here's the full class you can run.

import java.util.InputMismatchException;
import java.util.Scanner;

public class Sum {

    public static void main(String[] args) {
        float input = 0; 
        float sum = 0; 
        int count = 0; 
        boolean attempt = true;
        Scanner in = new Scanner(System.in); 

        while (attempt)
        {
            try
            {
                System.out.print("Enter a number: "); 
                input = in.nextFloat(); 
                sum += input; 
            }
            catch(InputMismatchException e)
            {
                in.nextLine();
                System.out.println("Not a number. Try again"); 
                count ++;
                if (count >=2)
                {
                  attempt = false; 
                }
            }
        }

        System.out.println("Sum: " + sum);
    }
}
takendarkk
  • 2,949
  • 7
  • 22
  • 35
  • Is this change really necessary? I had it work "as is", wrapping into a class and adding the imports. – Eric Platon Nov 29 '14 at 03:09
  • 1
    I did not get the same results. If I ran it "as it was" it would terminate after entering only 1 non number and the question stated "_When the exception occurs twice_". In other words, it never let me "Try Again". – takendarkk Nov 29 '14 at 03:10
  • 1
    Ok, super. The problem is originally that Tyler cannot even "get the program to even run in it's current state". Good call on the specs :-) – Eric Platon Nov 29 '14 at 03:12
  • I tried adding the in.nextLine() in the catch phrase, but my program still terminates instantly. When I run the program in Eclipse, it terminates instantly w/o even asking for a number. – Tyler Borchardt Nov 29 '14 at 03:55
  • Let me add the class/imports to my answer. Then if you copy/paste that and it still doesn't work, then the problem lies somewhere other than the code because I have tested this several times. – takendarkk Nov 29 '14 at 03:56
  • Takendarkk is right. Something with my setup. Also, Thank you for pointing my error in the catch exception. I definitely appreciate your quick response. I'm not good at programming so I'm very thankful for your help. – Tyler Borchardt Nov 29 '14 at 04:03