user1819438
user1819438

Reputation:

Scanner Class and InputMismatchException

while (!s.hasNextDouble())
{
System.out.println("not a value" );
    s.nextDouble();

}

sum = min = max = next = s.nextDouble();    

for (loop follows to decide min and max and average) 

Why do I get a scanner Exception in thread "main" java.util.InputMismatchException, when i Run this. I looked at the API and i think I am doing it right. If don't put the s.nextDouble() after the System.out then the loop runs fine, but as soon as type s.nextDouble(); the program crashes.

Upvotes: 1

Views: 105

Answers (1)

RobEarl
RobEarl

Reputation: 7912

You're trying to read a Double while you don't have one to read. Try:

while (!s.hasNextDouble())
{
    System.out.println("not a value" );
    s.next();
}
min = max = etc = s.nextDouble();

Upvotes: 3

Related Questions