aspose file tools*
The moose likes Beginning Java and the fly likes confused with in.readLine(); Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "confused with in.readLine();" Watch "confused with in.readLine();" New topic
Author

confused with in.readLine();

Dustin Schreader
Ranch Hand

Joined: May 25, 2009
Posts: 74
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.
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 8427
    
  23

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

Isn't it funny how there's always time and money enough to do it WRONG?
Articles by Winston can be found here
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 40052
    
  28
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

Joined: May 25, 2009
Posts: 74
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
Sheriff

Joined: Oct 13, 2005
Posts: 40052
    
  28
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
Sheriff

Joined: Oct 13, 2005
Posts: 40052
    
  28
It’s not readLine() but nextLine(). Sorry for the mistake.
Rumesh Eranga
Ranch Hand

Joined: Jan 26, 2011
Posts: 34
You can use Console object also(to read a line).

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

 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: confused with in.readLine();