Search code examples
javajava.util.scanner

Scanner doesn't see after space


I am writing a program that asks for the person's full name and then takes that input and reverses it (i.e John Doe - Doe, John). I started by trying to just get the input, but it is only getting the first name.

Here is my code:

public static void processName(Scanner scanner) {
    System.out.print("Please enter your full name: ");
    String name = scanner.next();
    System.out.print(name);
}

Solution

  • Change to String name = scanner.nextLine(); instead of String name = scanner.next();

    See more on documentation here - next() and nextLine()