• 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
  • Paul Clapham
  • Tim Cooke
  • Jeanne Boyarsky
  • Liutauras Vilda
Sheriffs:
  • Frank Carver
  • Henry Wong
  • Ron McLeod
Saloon Keepers:
  • Tim Moores
  • Frits Walraven
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Himai Minh

Receiving input question.

 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Marshal
Posts: 76395
364
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You can try nextLine but beware: you can get strange results if you use nextLine after nextAnythingElse.
 
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 76395
364
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
That which doesn't kill us makes us stronger. I think a piece of pie wouldn't kill me. Tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic