I run the program, but none of my lines execute. When I tell it to stop it prints a red error message.
Exception in thread "main" java.util.NoSuchElementException
at java.util.Scanner.throwFor(Unknown Source)
at java.util.Scanner.next(Unknown Source)
at java.util.Scanner.nextInt(Unknown Source)
at java.util.Scanner.nextInt(Unknown Source)
Here’s my code, nothing really seems to be out of the ordinary to my limited experience and my IDE doesn’t report any errors while I’m writing it.
import java.util.Scanner;
public class 312easf2
{
public static void main(String[] args)
{
Scanner keyboard = new Scanner(System.in);
int grade = 0; // initial value to satify loop condition
double averageGrade = 0.0;
int max = keyboard.nextInt();
int min = max;
int next = keyboard.nextInt();
System.out.println("Enter a nonnegative integer (negative to stop): ");
while(next >= 0);
{
if(next > max)
max = next;
else if(next < min);
min = next;
next = keyboard.nextInt();
}
}
}
EDIT: I point out a syntax gotcha in my answer, which you still have to fix before your program will work correctly, but for now the real problem lies in your first few lines:
Your program is executing something but it’s saying nothing, and printing the error only when you stop, all because of a mistake in your program’s implementation. It’s not a syntax error, however; it’s just that you forgot something important here.
Wcrousse in his answer explains what’s happening.
There
is a syntax issueare multiple similar syntax issues somewhere in your code that are actually legal syntax, which is why your IDE and the compiler aren’t complaining. However, they’re more often really a very common mistake that causes unintended behavior.Pay particular attention to your semicolons, and try looking through your code again. Or, use your debugger as Gabe suggests, that’ll give you a better idea what’s happening.