PDA

Click to See Complete Forum and Search --> : [RESOLVED] Simple string question


Darth Predator
Jan 30th, 2008, 02:49 PM
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

ComputerJy
Jan 30th, 2008, 03:57 PM
what do you use to read strings?

Darth Predator
Jan 30th, 2008, 05:46 PM
I am using the Scanner class, for instance


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.

Darth Predator
Jan 30th, 2008, 08:57 PM
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.

ComputerJy
Jan 30th, 2008, 10:23 PM
use Scanner.nextLine

Darth Predator
Jan 31st, 2008, 03:24 PM
Ah, there we go.

Thanks ComputerJy!