Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

difference between input.next(); and input.nextLine();  RSS feed

 
jon ninpoja
Ranch Hand
Posts: 291
3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi guys,

busy doing an adventure game exercise off programming by doing...love it by the way...really recommend it to fellow green horns!!!

here is my code:



near the top th line
String startGame = input.nextLine();

was originally
String startGame = input.next();

this made my first statements print twice...

so i was getting

You are standing in front of the old house..."enter" to go in, or you can "leave"

twice ...and

wasn't one of the options. Try again."

was also printing...then th program ran as expected

changing the next to nextLine fixed the problem

why was this happening...

thanks for reading
 
Norm Radder
Ranch Foreman
Posts: 2240
28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The next() method leaves the lineend character from pressing the Enter key in the Scanner class's buffer, the nextLine() method reads everything including the lineend character.
If the nextLine() method is called after the next() method, it will read the lineend that was left in the buffer by next() and return an empty String.
 
William Brogden
Author and all-around good cowpoke
Rancher
Posts: 13078
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Looking at the JavaDocs, it appears that the next() method "finds and returns the next complete token" where nextLine() looks for everything up to a line separator.

I think that next() saw two tokens - a typed word and a cr character.  The cr character triggered the not an option branch.

Bill
 
jon ninpoja
Ranch Hand
Posts: 291
3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks guys,

sorry...so when would you use next and nextline? or do you typically use nextline most of the time
thanks again
 
Norm Radder
Ranch Foreman
Posts: 2240
28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
when would you use next and nextline?

Use next when you want to read the next token. Tokens are separated by delimiters like spaces or lineend characters.
Use nextLine when you want to read everything up to and including the lineend character.  There can be spaces etc in the line that is read.
 
Ganesh Patekar
Bartender
Posts: 696
23
Eclipse IDE Hibernate Java jQuery MySQL Database Netbeans IDE Oracle Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
See the difference between next and nextLine reading input then you can decide according to your need.
Output:
next(): Being
next(): a
nextLine(): Being a Java programmer
 
Campbell Ritchie
Marshal
Posts: 55751
163
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry I am late to this discussion because I have been away.
I can see all sorts of confusion. Only use \n or \r if somebody has told you to use the CR or LF characters, never if you want an ordinary line end. Use println(...) or printf("...%n...") instead. Try to join your text to be printed together with the + operator rather than printing individual tokens or lines; you will get slightly faster execution. Your code is much too long to fit into the main method.

You might get different results from a Scanner if you use it at the keyboard from if you use it with a String. There is serious confusion about nextLine in many books, but the API documentation is quite clear: it returns
the rest of the current line
You can get the empty String returned if you use nextLine after nextAnythingElse. There is more explanation and one possible solution to that problem here.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!