• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

reading strings with readLine

 
Bron Czimes
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 83
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1272
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Bron Czimes
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 7729
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Barry Gaunt
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 44
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 44
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
NetBeans gives me the same result.
 
Ben Buchli
Ranch Hand
Posts: 83
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
vhey bron can you post the code that worked besides the age.
 
Bron Czimes
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 83
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
alright, copied your writePassengers() code and ran it. it works fine.
what kind of jre version are you using?
Ben
 
Bron Czimes
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
the latest one
 
Bron Czimes
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 44
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 7729
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic