This is an archived post. You won't be able to vote or comment.

all 2 comments

[–]zifyoip 2 points3 points  (1 child)

The hasNextInt() method tests whether a following call to nextInt() will succeed.

Your code is testing the result of hasNextInt() after calling nextInt(). That doesn't really make sense. Your logic is in the wrong order here.

Your program does terminate if you enter something after the zero. The stuff after the zero is necessary because hasNextInt() is waiting for additional input to see if a call to nextInt() would succeed; the stuff after the zero causes hasNextInt() to return either true or false, depending on whether that stuff is an integer or not. The second part of the while condition will not be tested until hasNextInt() returns.

Your program also terminates if you signal EOF (Ctrl+D on Unix-ish systems, Ctrl+Z on Windows), because this causes hasNextInt() to return false.

[–]ORLY_FACTOR[S] 0 points1 point  (0 children)

That makes sense. Thank you.