Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Choice?  RSS feed

 
Christian Long
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
uh Hello, I need some help.


I'm stuck here.. When it says "Invalid choice, please try again" - is there a way(maybe through loops) for the user to 'choose again'?
 
Peter Chase
Ranch Hand
Posts: 1970
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You want a "while" loop.
 
Christian Long
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
oh, I already tried using a 'while loop' but i could not achieved anything - can someone help me further please..
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Have you displayed your variable s to see what you really read? This displays the input as int and as char:

I'm guessing you will be surprised, and you might like a BufferedReader and readLine better. Let us know what you find.
[ October 23, 2007: Message edited by: Stan James ]
 
Christian Long
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
1. I have found that it's possible to do what I need using a while loop & simple in.read().
2. However, using the readLine method would be complex as it would return the result as a 'String'. I would need some 'Pseudo' coding like this:

Even here there will be a problem - So, I think I'll stick to using a while loop & a simple in.read().
[ October 23, 2007: Message edited by: Christian Long ]
 
Peter Chase
Ranch Hand
Posts: 1970
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As Stan says, you will find that in.read() does not do what you hope. Try entering more than one character, for instance!

Doing it properly ain't so hard. Here's my effort: -



By the way, I know we don't normally post complete code, but I thought it appropriate in this circumstance.

Oh, yea, I noticed I used a "for" loop, not "while". Most loops can be written with either, and it's a case of which one looks nicer in a particular case.
[ October 23, 2007: Message edited by: Peter Chase ]
 
Christian Long
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Peter Chase.I just learned a lot more by searching the internet. Your example showed me that it was quite easy infact.

However, There's still 1 something I don't seem to be understanding:
try { r.close(); }

Does the BufferedReader need to be close (as above)?
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In this case, since the program is ending anyway, probably not. And the consequences of failing to close an input stream are generally less severe than for an output stream. But it's a good practice in general to always close streams when you're done with them. That way you'll be safe even if the code is refactored later to try to do something else after you're done reading, and/or to read from something other than stndard input.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!