• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Bear Bibeault
  • Junilu Lacar
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • salvin francis
  • Frits Walraven
Bartenders:
  • Scott Selikoff
  • Piet Souris
  • Carey Brown

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

 
Ranch Hand
Posts: 424
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
 
Master Rancher
Posts: 3921
36
  • 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.
 
Author and all-around good cowpoke
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: 424
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
Master Rancher
Posts: 3921
36
  • 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.
 
Bartender
Posts: 1248
86
Hibernate jQuery Eclipse IDE Angular Framework Spring MySQL Database AngularJS Tomcat Server Java
  • 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
 
Marshal
Posts: 70211
280
  • 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.
 
Replace the word "snake" with "danger noodle" in all tiny ads.
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
    Bookmark Topic Watch Topic
  • New Topic