Issue with java program runtime

Ð88

Fully Optimized
Messages
4,775
I went back to an old program I was working on and now it won't work at runtime.

The issue starts right at the beginning where I have the statement:

Code:
Scanner in = new Scanner(System.in);

The program pauses here until I hit enter. Now the main issue occurs when the first input request is called which asks for an integer but, as I am assuming it to be, a '\n' is appended to the beginning of the input from when I had to hit enter so now I get an error:

Exception in thread "main" java.util.InputMismatchException: "" is not an integer

I know it stems from when i have to hit enter. The program never used to pause at this spot before but now suddenly it does. Any ideas?
 
It's not to do with \n being appended anywhere - calling nextInt() with a blank string will always result in an InputMismatchException because an empty string can't sensibly be parsed as an integer.

If you want to guard against this (and other illegal entries) then check using the "hasNextInt" method on the scanner object first. If it returns true you can go for it, otherwise you know to halt and do something else.
 
^^ Assuming that's already being done, I'd reinstall your JDK and see if that helps at all.
 
Geez...I tried your fix, berry, and while that worked (sort of) I ended up with so many other runtime problems. I don't what the hell happened but now functions no longer work properly and the program is basically set to be abandoned now.

It's not important that it works now. It was actually a term project for a class a year or so ago that I was hoping to improve on for just the heck of it. It worked at the time I submitted it so that's all that matters. Oh well....new project!
 
Feel free to send it my way if you like - I like a good Java puzzle to solve now and again ;)
 
Back
Top Bottom