• 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
  • Tim Cooke
  • Devaka Cooray
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Martijn Verburg
  • Frits Walraven
  • Himai Minh

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: 76885
366
  • 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: 76885
366
  • 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: 76885
366
  • 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 : ");

 
You guys haven't done this much, have ya? I suggest you study this tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic