Results 1 to 4 of 4
  1. #1
    ngc0202 is offline Member
    Join Date
    Jul 2009
    Posts
    32
    Rep Power
    0

    Default Scanner.next() problems!

    I am writing a code that requires multi-word input. Here's the code I used:
    Java Code:
    while (true) {
          out.print("> ");
          userInput = keyboard.next();
     }
    When the user inputs multiple words, however, it gets all funny:
    > oneword
    > two word
    > >
    Is there any way to fix the above problem?

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    You're having trouble dealing with the end of line token and as such, you may do better getting nextLine() rather than next().

  3. #3
    ngc0202 is offline Member
    Join Date
    Jul 2009
    Posts
    32
    Rep Power
    0

    Default

    Nice, thank you, I knew it would be an easy fix :)

  4. #4
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,339
    Rep Power
    25

    Default

    If you want to read the words one at a time, use next(). Using next() allows the user to pre-answer questions by typing them all on one line. next() will read them one at a time.

    If you want to read all the words and the newline char at the end, use nextLine()

Similar Threads

  1. Using scanner for CSV
    By getName() in forum Advanced Java
    Replies: 7
    Last Post: 06-20-2010, 04:33 PM
  2. Help With Scanner
    By jtmoney0511 in forum New To Java
    Replies: 10
    Last Post: 10-12-2009, 11:24 PM
  3. Scanner
    By choko in forum New To Java
    Replies: 10
    Last Post: 01-24-2009, 03:37 PM
  4. Scanner class problems
    By sypherz in forum New To Java
    Replies: 6
    Last Post: 06-09-2008, 09:46 AM
  5. need help with scanner
    By whiterex in forum New To Java
    Replies: 1
    Last Post: 04-22-2008, 01:41 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •