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

Receiving input question.  RSS feed

 
Bp Parker
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello again forums, i've been around here for a few days now
been re-writing small programs daily trying to get down the basics
of this while learning the fundamentals of Java. Anyways I have
a quick question. In the code I have below


It lets me input text and stores it in the name var for me.
It only stores the first Word I type though. Exp,
"Hello, what is your'e name?" Kara Kodet
"Ok, so you're name is Kara."


Could someone help me with what would I use if
I wanted it to print out all the text a user would type
so therefor, it would print out
"Ok, so you're name is Kara Kodet."

Edit: Oh, & the new beta view, is Super Sexy had to mention! (: Good Job
Can't wait to see the final release.
 
Campbell Ritchie
Marshal
Posts: 55711
163
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can try nextLine but beware: you can get strange results if you use nextLine after nextAnythingElse.
 
Winston Gutkowski
Bartender
Posts: 10573
65
Eclipse IDE Hibernate Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bp Parker wrote:It lets me input text and stores it in the name var for me.
It only stores the first Word I type though.

Because next() gets the next token; and by default Scanner delimits its tokens with whitespace.

As Campbell said, you can use nextLine(), which will return everything you type up to the point you hit the 'enter' key.
Personally, it's the only method I use for keyboard input, because it's very straightforward and it always leaves the Scanner in a state to accept another line - but many people (including Campbell ) disagree with me on this.

Winston
 
Bp Parker
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ah thanks very much guys. That worked for me this time although i'll
keep in mind it might mess up results for future use i'll just keep a close
eye on it & how I use it.
 
Campbell Ritchie
Marshal
Posts: 55711
163
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well done

If it does mess up, there is a good chance that somebody here will know how to sort it out. We have seen it before.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!