Results 1 to 10 of 10
Thread: Skipping an Integer
- 03-29-2011, 09:40 AM #1
Member
- Join Date
- Mar 2011
- Posts
- 8
- Rep Power
- 0
Skipping an Integer
Hi guys,
I wonder if you can help me. I am currently writing an assignment program that needs to do one thing if an integer is entered and another if it is not.
The problem is where i prompt the user for input; if i do enter an integer all is fine but if i don't enter an integer, and just hit enter, the program goes to the next line and hangs until something is entered (an int, and then moves on or a character and then throws an exception).
Any help would be greatly appreciated, i have looked on the net and in my text books but i cant find anything. I am using Scanner for the input.
BM
- 03-29-2011, 09:44 AM #2
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
I'm not quite sure what you want, can you clarify?
- 03-29-2011, 09:52 AM #3
Member
- Join Date
- Mar 2011
- Posts
- 8
- Rep Power
- 0
Hi,
The program prompts the user for input on a range of items, one is an integer that represents a number of days. If the user does not need to enter any extra days, they are to simply leave this blank and hit enter and go to the next item. Its a simple print line and store user input in variable through using Scanner.
My problem is when i don't enter anything and just hit enter, the program just hangs, if i press enter again, it just hits a new line and hangs, until something is entered.
BM
- 03-29-2011, 10:00 AM #4
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,545
- Rep Power
- 11
Perhaps you could post some code?
One thing I would observe is that if you want to know if the user entered an int on a line or something else (where something else includes nothing at all), then using nextInt() is wrong. (See its API documentation)
- 03-29-2011, 10:29 AM #5
Member
- Join Date
- Mar 2011
- Posts
- 8
- Rep Power
- 0
The code is :
If nothing is entered for a string the program just moves to the next item and null is recorded, but if nothing is entered for the integer, the program just keeps moving to a blank new line, until a value is entered in.Java Code://prompt user for input System.out.print("Enter your assement: "); String assesment = input.nextLine(); System.out.print("Enter class:"); String studentClass = input.nextLine(); System.out.print("Enter days needed: "); Int daysNeeded = input.nextInt(); // this is where it hangs if an if(input.hasNextLine()){ // integer is not entered input.nextLine(); } System.out.print("Enter Teacher: "); String teacher = input.nextLine()
BM
- 03-29-2011, 08:18 PM #6
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,545
- Rep Power
- 11
If nothing is entered for a string the program just moves to the next item and null is recorded, but if nothing is entered for the integer, the program just keeps moving to a blank new line, until a value is entered in.
Exactly. That is the documented behaviour for nextLine() and nextInt(). Briefly nextLine() "Advances this scanner past the current line and returns the input that was skipped" ie it returns a (possibly empty) line. On the other hand nextInt() "Scans the next token of the input as an int" ie it keeps ignoring empty lines until it gets a token which it returns as an int.
Is this the actual code you are using? I ask because it has a typo ("Int" for int) and because it does not even attempt what you stated originally: that the program "needs to do one thing if an integer is entered and another if it is not."
If you are trying to see if the user entered an int or something else (including entering nothing at all) then don't use nextInt(). Fundementally this is because you are not trying to read an int: rather you are trying to read something that may or may not be an int. Read the whole line instead and decide, from its contents, whether the user entered an int.
The Integer method parseInt() may help.
- 03-29-2011, 09:28 PM #7
Member
- Join Date
- Mar 2011
- Posts
- 8
- Rep Power
- 0
Thanks for that pbrockway2,
You have answered my question completely, and have pointed me in the right direction, so that i can now look not so much for an int but rather if there is an integer.
I really appreciate your time in addressing my question.
BM
- 03-30-2011, 12:40 AM #8
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,545
- Rep Power
- 11
You're welcome.
If you get stuck obtaining the user's input, deciding whether it's an integer and doing one thing or another based on that, post whatever you come up with. I didn't post "teh codez", but only so you weren't deprived on the fun of writing your own solution, not to close off discussion with you left stuck.
- 03-30-2011, 02:38 AM #9
Member
- Join Date
- Mar 2011
- Posts
- 8
- Rep Power
- 0
I think the best part about this forum, are the people like yourself that help "point you in the right direction". I would much rather have someone give me tips on where to look and why something might be behaving a certain way, then justing pasting code and telling me that this will "fix the situation". At the end of the day its all about learning to do the code yourself, but with a little nudge in the right direction from people with more experience.
Once again,
thanks :)
- 03-30-2011, 04:54 AM #10
That's good to hear. Unfortunately you are in the minority. I have seen on several occassions where someone has copied and pasted the spoonfed code directly into their code and were surprised when it didn't work and post back to whine about it. If they bothered to tink about it and actually learn then they would have been able to adapt the code to suit their needs.
Similar Threads
-
Skipping Scanner
By chari_autumn in forum New To JavaReplies: 2Last Post: 01-04-2011, 06:29 AM -
convert unsigned integer to signed integer in java?
By diskhub in forum New To JavaReplies: 6Last Post: 05-17-2010, 12:50 AM -
Skipping links!!!
By jvh in forum New To JavaReplies: 2Last Post: 03-04-2010, 12:11 AM -
Skipping lines during debugging
By Taxi in forum New To JavaReplies: 2Last Post: 12-27-2008, 03:53 AM -
skipping input
By windie_86 in forum New To JavaReplies: 1Last Post: 02-12-2008, 07:09 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks