• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

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

 
Ranch Hand
Posts: 424
3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Rancher
Posts: 5008
38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Author and all-around good cowpoke
Posts: 13078
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 424
3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Rancher
Posts: 5008
38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Bartender
Posts: 1251
87
Hibernate jQuery Spring MySQL Database Tomcat Server Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Marshal
Posts: 79179
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic