If the user enters a blank space as their input, how would you read that as a " " string?

Viewed 42
//Example:
Scanner scanner = new Scanner(System.in);
String character = scanner.next();

//User hits the space bar and hits enter
System.out.println(character);

//character should = " "
1 Answers

scanner.next() gives you the next complete token, which is defined as follows:

A complete token is preceded and followed by input that matches the delimiter pattern.

If you want the space, I'm assuming you want the whole line. You should either tell the scanner to get the entire line or set the delimiter of the scanner to the line break character. See scanner.nextLine() and scanner.useDelimiter().

Related