• Post Reply Bookmark Topic Watch Topic
  • New Topic

nextLine()  RSS feed

 
Davey Lopez
Ranch Hand
Posts: 33
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm having trouble understanding what this method actually does.

nextline()


My book keeps saying that it advances the scanner past the current line and returns input that was skipped.
Not really sure what that means or why it would be used.
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16060
88
Android IntelliJ IDE Java Scala Spring
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Do you understand what class java.util.Scanner does? Have you looked at the API documentation?

A Scanner reads text from some input source, for example the console (System.in). It is commonly used for reading things that a user types in in the console window. The nextLine() method reads until it finds a newline character in the input. If you're using a Scanner to read from the keyboard, this method will read the text that the user enters until the Enter key is pressed. It returns the text that was entered in a String.

Here's a simple example:
 
Davey Lopez
Ranch Hand
Posts: 33
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ah i see now.it's so simple
-thanks again
 
Campbell Ritchie
Marshal
Posts: 56592
172
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Not quite as simple, I am afraid. There is a nasty pitfall. Read this. That pitfall occurs if you call nextLine after nextAnythingElse.
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:Not quite as simple, I am afraid. There is a nasty pitfall...

@Davey: Just one of the reasons I'm not a big fan of Scanner.

One way to get around it, that I personally like, is to always use nextLine(). That way, you always get a String - even if it happens to contain a number - and then you can decide how you want to break it down further (or convert it).
Furthermore, the method doesn't throw any Exceptions, and always leaves the Scanner in a state ready to read another line. It's when you start mixing input methods that you run into problems.

And remember: you can create a Scanner from a String, not just a Stream; so you can always create a second one to parse your 'line'.

I'm sure the Scanner-philes will flame me, but it's never steered me wrong yet.

Winston
 
Campbell Ritchie
Marshal
Posts: 56592
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
We won't flame you. We might disagree, but never flame.
 
Robert D. Smith
Ranch Hand
Posts: 221
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Winston Gutkowski wrote:
Campbell Ritchie wrote:Not quite as simple, I am afraid. There is a nasty pitfall...

@Davey: Just one of the reasons I'm not a big fan of Scanner.
I am curious about this comment, Winston. To what alternatives do you allude? By your comment regarding scannerphiles, I am under the impression that you prefer another input methodology. I have found myself using scanner and nextLine() for my user input (I don't use any of the other methods since I know I'm going to get a string and can then deal with any data exceptions). But if there is an alternative, ...

Regards,
Robert
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Robert D. Smith wrote:But if there is an alternative, ...

Sure. Pretty much any Reader (I used BufferedReader myself, because it also has a readLine() method). System.in is just an InputStream.

Scanner was only introduced in 1.5, so what do you think people did before then?

One of the things I do like about Scanner is that you don't have to deal with all those damn checked Exceptions, but the API is a bit overblown IMO - and the 'gotchas' not very well explained.

Winston
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!