• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Removing line buffer

 
David Martinson
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,

This is my first post. I'm reading Oracle's Beginning Java 5th edition. If you can suggest something better to start with, I'm all ears....or horns. :-) In any event, can someone better explain removing the line buffer? This is what I'm working with.



Obviously I don't have the whole practice example, but this line specifically I'm having a difficult time wrapping my head around. According to the book, when you have input then press ENTER, a carriage return, line feed sequence is entered into the input stream. Furthermore, these characters are left pending in the input buffer until you read them. Q: Do they mean when you hit enter? Is that supposed to be a 'read'? Apparently this can cause problems. The code above is to remove the line. There has to be a better way of explaining this. I'm definitely a visual person. I'm trying to imagine how this would look.

Thanks,
K
 
Paul Clapham
Sheriff
Posts: 21111
32
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You want visual? Okay, let's try this.

Imagine a conveyer belt. At one end is you with your keyboard. Every time you press a key, that key's character falls onto the belt and gets carried along. When you press the Enter key, then... what it said in the book. Those characters fall onto the belt and get carried along.

At the other end is your Java program. Every time it calls the System.in.read() method, it takes the next character off the conveyer belt and gives it to your program.

So that's what the business about the "input buffer" means. Does that help you visualize the process?
 
Campbell Ritchie
Sheriff
Pie
Posts: 49361
62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch
Don’t use the read() methods. Try a Scanner, which can read a whole line, or an individual token. There is a pitfall about its nextLine() method, however. You can find a tutorial if you search: start searching here. There is an example which is probably very close to what you want in the Scanner documentation.
 
Campbell Ritchie
Sheriff
Pie
Posts: 49361
62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
David Martinson wrote: . . . when you . . . press ENTER, a carriage return, line feed sequence is entered into the input stream. . . .
That bit is, I think misleading. It only applies to windows®. If you use a Unix/Linux box, you only get line‑feed, and if you use an old Mac you only get carriage‑return. (Newer Macs use the same as Unix).
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic