*
The moose likes Beginning Java and the fly likes reading strings with readLine Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "reading strings with readLine" Watch "reading strings with readLine" New topic
Author

reading strings with readLine

Bron Czimes
Ranch Hand

Joined: May 09, 2004
Posts: 44
ok guys, more problems (woohoo)
I'm trying to read some input that the user will give me using readLine method from the bufferedReader class, but it skips over it and won't let the user actually enter any data.
so the code is

when run, this returns
Passenger name:
Age of passenger:

what the??
Bron
Ben Buchli
Ranch Hand

Joined: Mar 26, 2004
Posts: 83
Well, first of all I assume that you have declared your variables beforehand.
Your System.in doesnt work, so read the age as a string and then parse it with Integer.parseInt( String ). That way you can also assure that the input is valid, eg. an integer.
Same of course is true for the newSeat value. also you want to make sure that the age is between lets say 1 and 120, and the seat value is greater than one but smaller or equal to 20.
other than that, your code works just fine...
when i ran your code, slightly modified:

this was my output:
Ben 28 M 1
notice that I could have entered "bla" for age and it wouldnt complain. so use a while loop to display the "enter age" message for as long as not an int is entered.
hope that helps.
Ben
[ May 11, 2004: Message edited by: Ben Buchli ]
Mike Gershman
Ranch Hand

Joined: Mar 13, 2004
Posts: 1272
I couldn't get your exact error, but System.in.read() reads one character and does not then eat the following '\n' so it causes the next line to be read as an empty String, so you will seem to get no chance to respond to a prompt.


Mike Gershman
SCJP 1.4, SCWCD in process
Bron Czimes
Ranch Hand

Joined: May 09, 2004
Posts: 44
yeah, thats the error i'm getting Mike. the user has no chance to enter anything.
how did you manage to enter your name and stuff Ben coz I have the exact same code and it just won't let the user do anything.....
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
Sorry to be pedantic Bron, but did you really copy Ben's code and run it? The first code you presented is not the same as Ben's.
If so, what operating system are you running the program on?


Ask a Meaningful Question and HowToAskQuestionsOnJavaRanch
Getting someone to think and try something out is much more useful than just telling them the answer.
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
Funny, in JEdit I get this when running the program in the JEdit console:


With the DOS prompt (cmd window) I get:

So how are you running it Bron?
Bron Czimes
Ranch Hand

Joined: May 09, 2004
Posts: 44
when i run Ben's code, i get this
Passenger name:
Age of passenger:
I enter a name and press enter, then i get

Gender of passenger:
enter gender, press enter
Preferred seat (between 1 and 20):
Bronwyn F 2

so it doesn't like the age thing.

I'm running winXP Barry, and I'm running it through a Gel IDE. Haven't managed to do it through DOS yet.
Bron Czimes
Ranch Hand

Joined: May 09, 2004
Posts: 44
NetBeans gives me the same result.
Ben Buchli
Ranch Hand

Joined: Mar 26, 2004
Posts: 83
vhey bron can you post the code that worked besides the age.
Bron Czimes
Ranch Hand

Joined: May 09, 2004
Posts: 44
yep, sorry for long delay, i'm in a different time zone to you guys.

this is exactly what i'm running.
then without the age stuff

really, its not the age (i think) that it doesn't like coz it will kinda do that. its just whatever is first in line, be it age or passengers name, it will skip over.
this is driving me nuts.
haha
thanks for helping me guys
Bron
Ben Buchli
Ranch Hand

Joined: Mar 26, 2004
Posts: 83
alright, copied your writePassengers() code and ran it. it works fine.
what kind of jre version are you using?
Ben
Bron Czimes
Ranch Hand

Joined: May 09, 2004
Posts: 44
the latest one
Bron Czimes
Ranch Hand

Joined: May 09, 2004
Posts: 44
ok, i took it away from the rest of my project, and ran it in a completely new project and system, and it works fine. so something must be up with some other code somewhere, or my system must be screwed or something.
oh great.
Bron
Bron Czimes
Ranch Hand

Joined: May 09, 2004
Posts: 44
ok, i feel very silly right now, but i've worked it out.
i hadn't included a system.in.skip in the previous method which meant that an error in that method was carried through to this method. D'oh!!!
THANKYOU SO MUCH Ben, Mike and Barry for your help. Sorry to have wasted your time.
woohoo
Bron
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
No problem Bron. If you come back a few times and help someone once in a while, then your "debt" is paid.
[ May 13, 2004: Message edited by: Barry Gaunt ]
 
 
subject: reading strings with readLine
 
Similar Threads
reading csv file
Priority Queue doubt
Entering many records..Need help!
copying arrays - please h e l p!
StringBuffer