I want to take the input from a user, in this case, their full name and split it with the .split(" ") command into 2 or 3 sections, depending on number of names, and then print out the results on different lines:
For example,
User inputs:
"John Doe Smith"
Program returns:
John
Doe
Smith
Currently, my code looks like this:
import java.util.Scanner;
public class Task2 {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
String name;
System.out.println("Enter your full name: ");
name = input.next();
String[] parts = name.split(" ");
System.out.println(java.util.Arrays.toString(parts));
}
}
This currently only returns the first part of the name.
The program should also be able to deal with someone not having a middle name.