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);
}
}