Go Back   Computer Forums > General Computing > Programming
Click Here to Login
Join Computer forums Today


Reply
 
Thread Tools Search this Thread Display Modes
 
Old 02-19-2010, 05:44 PM   #1
Fully Optimized
 
Š88's Avatar
 
Join Date: May 2005
Posts: 4,775
Send a message via MSN to Š88
Default Issue with java program runtime

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:

Quote:
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?
__________________

__________________
Killing under the cloak of war is no different than murder

Kein Alkohol ist auch keine Lösung!
Š88 is offline   Reply With Quote
Old 02-19-2010, 08:52 PM   #2
Site Team
 
berry120's Avatar
 
Join Date: Jul 2009
Location: England, UK
Posts: 3,425
Default Re: Issue with java program runtime

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.
__________________

__________________
Save the whales, feed the hungry, free the mallocs.
berry120 is offline   Reply With Quote
Old 02-19-2010, 09:32 PM   #3
Site Team
 
celegorm's Avatar
 
Join Date: Sep 2006
Posts: 10,713
Send a message via AIM to celegorm
Default Re: Issue with java program runtime

^^ Assuming that's already being done, I'd reinstall your JDK and see if that helps at all.
__________________
"as a fanboy i refuse to admit it and will pull countless things out of my butt to disprove it"

Team Thelegorm! Total Kills: 21 (i iz in uor profile, editsing your sigz)
celegorm is offline   Reply With Quote
Old 02-22-2010, 02:34 PM   #4
Fully Optimized
 
Š88's Avatar
 
Join Date: May 2005
Posts: 4,775
Send a message via MSN to Š88
Default Re: Issue with java program runtime

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!
__________________
Killing under the cloak of war is no different than murder

Kein Alkohol ist auch keine Lösung!
Š88 is offline   Reply With Quote
Old 02-23-2010, 05:23 AM   #5
Site Team
 
berry120's Avatar
 
Join Date: Jul 2009
Location: England, UK
Posts: 3,425
Default Re: Issue with java program runtime

Feel free to send it my way if you like - I like a good Java puzzle to solve now and again
__________________
Save the whales, feed the hungry, free the mallocs.
berry120 is offline   Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off



All times are GMT -5. The time now is 08:30 AM.


Powered by vBulletin® Version 3.8.8 Beta 4
Copyright ©2000 - 2017, vBulletin Solutions, Inc.
Search Engine Friendly URLs by vBSEO 3.6.0