Ask your own question, for FREE!
Computer Science 12 Online
OpenStudy (anonymous):

i'm trying to learn some stuff and i'm having trouble. my code looks like this: import java.util.Scanner; public class MessingAround { public static void main(String[] args) { String s; Scanner a = new Scanner(System.in); System.out.println("Enter a string."); s = a.next(); System.out.print("You said: " + s); } } it prints only the first word of any string i enter. ex) Enter a string. bow ties are cool You said: bow help?

OpenStudy (anonymous):

i changed s = a.next(); to s = a.nextLine(); i think that's right because it works now

OpenStudy (pradius):

yes, that was the error.

OpenStudy (rsmith6559):

A scanner is going to break a string into tokens. Calling a.next() once will get one token. You'd either need to loop through the tokens, print it and add a separator character, or do what you did which kind of makes the scanner unneeded.

Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!
Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!