• Post Reply Bookmark Topic Watch Topic
  • New Topic

java Scanner  RSS feed

 
chamini prashakthi
Ranch Hand
Posts: 127
3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can some one expalin me why we have to use
scan.nextLine();
String s=  scan.nextLine();

in following code



in integers and doubles we have to put int/double a= scan.next...();
why it is different for string when print a sentence?
 
Carey Brown
Saloon Keeper
Posts: 3309
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"nextDouble()" reads characters until it finds a character that is not part of a patterns for a double. Unfortunately if this is the last input before a new-line character then the new-line gets left in the input stream. "nextLine()" will consume this new-line so that the subsequent call to "nextLine()" will return the complete next line.
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!