• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

confused with in.readLine();

 
Ranch Hand
Posts: 74
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ok this sounds simple enough but here is my problem, when I try to choose option 6 I prompt the console to have an item entered but no input text option pops up it just goes straight into prompting to Please enter price and just skips over entering item. Not really sure what I'm doing wrong here.
 
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Dustin Schreader wrote:Not really sure what I'm doing wrong here.


Well your title asks about readLine(), but you're running read().

If you want the first, you have to to wrap System.in in a BufferedReader:
BufferedReader keyboard =
   new BufferedReader(new InputStreamReader(System.in));


A bit ugly, I know (I wish Java would fix that).

readLine() also returns a String, so you need to pull the first character from it:
cmd = keyboard.readLine().charAt(0);

It should be noted that with readLine(), the character won't be read until you press the Enter key.

Another alternative, popular with beginners, is java.util.Scanner.

Winston
 
Marshal
Posts: 79153
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I’d go for Scanner.

Beware of read(); it returns the last character as an int and is not at all user-friendly.
 
Dustin Schreader
Ranch Hand
Posts: 74
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I got it to work with the read(); thanks for the help. I tried it with the Scanner but I got the same problem.
 
Campbell Ritchie
Marshal
Posts: 79153
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That is because the Scanner#readLine() method doesn’t do what you think it does. Look here for discussion of a similar problem.
 
Campbell Ritchie
Marshal
Posts: 79153
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It’s not readLine() but nextLine(). Sorry for the mistake.
 
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You can use Console object also(to read a line).

Console c = System.console();
String myString = c.readLine("Please enter a text to read : ");

 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic