tags:

views:

144

answers:

3

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

}
+12  A: 

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:

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

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 issue are 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.

BoltClock
Nice leading answer.
Kirk Woll
It would be constructive to single-step through your code in a debugger to see exactly what is executing.
Gabe
@Kirk Woll: thanks, this is my first shot at a [homework] answer :)
BoltClock
Thanks a lot! I'll work on paying more attention next time. Do you all have a good resource for learning how to use a debugger?
Breedly
Your IDE's documentation may have some pointers.
BoltClock
A: 

I would recommend you to use some nice IDE with code analysis features. It will make your life easier and will help you to avoid such annoying mistakes wich can drive you nuts because you just made a stupid typo and can't figure out what is the problem.

Look at IntelliJ IDEA Community Edition (it's free) or at Eclipse (but I'm not sure about automatic code analisys in Eclipse, I don't use it).

P.S.

 while(next >= 0);

remove the semicolon and it should do something..

Roman
+3  A: 

semicolon aside, I wonder if you realize that the program, as you have written it, requires the user to enter a number for max and another number for next before it prints the message, "Enter a nonnegative integer (negative to stop): ." It then takes another number for next and enters the loop. Because the prompt for a number is outside the loop, it requires the user to continue entering numbers without printing anything further.

Wcrousse
I'm pretty sure OP's given code is a WIP, but good reminder.
BoltClock
@Bolt: I don't see why the extraneus semicolons would prevent the program from executing any lines, as the question states. of course the program won't run as intended, but it should take two values and ask for another number before things go wrong. therefore, I suspect that Breedly didn't realize that the program was running but waiting for an input.
Wcrousse
@Wcrousse: I finally get what you mean. +1 and edited my answer to refer to yours.
BoltClock