Results 1 to 6 of 6

Thread: [RESOLVED] Simple string question

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Jul 2004
    Posts
    104

    Resolved [RESOLVED] Simple string question

    Hi all,

    I just started learning Java and I have a question about user input strings. It seems that when you type in a string of multiple words, it only picks up the first word as the entire string and ends it when it reaches the first space. How do you type in a string with multiple words and have it pick up the entire sentence as one string?

    Thanks

  2. #2
    Arabic Poster ComputerJy's Avatar
    Join Date
    Nov 2005
    Location
    Happily misplaced
    Posts
    2,513

    Re: Simple string question

    what do you use to read strings?
    "I'm not normally a praying man, but if you're up there, save me... Superman!" - Homer Simpson
    My Blog

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Jul 2004
    Posts
    104

    Re: Simple string question

    I am using the Scanner class, for instance

    Code:
    String a;
    Scanner sc = new Scanner (System.in);
    System.out.print("Type sentence here: ");
    a = sc.next();
    If you enter say "Good morning" at the prompt, it returns only "Good" as the string instead of both words.

  4. #4

    Thread Starter
    Lively Member
    Join Date
    Jul 2004
    Posts
    104

    Re: Simple string question

    Okay, I figured out how to make it work using the BufferedReader readLine() method. However, I still would like to know if and how it is possible to do using the Scanner class and the variable.next() method.

  5. #5
    Arabic Poster ComputerJy's Avatar
    Join Date
    Nov 2005
    Location
    Happily misplaced
    Posts
    2,513

    Re: Simple string question

    use Scanner.nextLine
    "I'm not normally a praying man, but if you're up there, save me... Superman!" - Homer Simpson
    My Blog

  6. #6

    Thread Starter
    Lively Member
    Join Date
    Jul 2004
    Posts
    104

    Re: Simple string question

    Ah, there we go.

    Thanks ComputerJy!

Posting Permissions

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



Click Here to Expand Forum to Full Width