Scanner doesn't see after space

igeer12 picture igeer12 · Oct 22, 2013 · Viewed 84.1k times · Source

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);
}

Answer

Prabhakaran Ramaswamy picture Prabhakaran Ramaswamy · Oct 22, 2013

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

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