wood burning stoves
The moose likes Beginning Java and the fly likes Removing line buffer Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Java Interview Guide this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Removing line buffer" Watch "Removing line buffer" New topic

Removing line buffer

David Martinson

Joined: Dec 12, 2012
Posts: 1

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.


Learning from: Java...A Beginner's Guide, 5th Edition. Oracle Press.
Paul Clapham

Joined: Oct 14, 2005
Posts: 19973

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

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

Joined: Oct 13, 2005
Posts: 46412
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).
I agree. Here's the link: http://aspose.com/file-tools
subject: Removing line buffer
It's not a secret anymore!