Question: When a user enter more then one space my program doesn't properly print the users name out, for example if the user prints first name followed by 2 space and then last name the middle name then prints as the spaces and the last nmae prints?? how can I improve on this issue so the extra space a user enters isn't counted as a middle or last name??

```
public static void main(String[] args)
{
    Scanner sc = new Scanner(System.in);

    System.out.println("Welcome to the name parser.\n");
    System.out.print("Enter a name: ");
    String name = sc.nextLine();  

    name = name.trim();

    int startSpace = name.indexOf(" ");
    int endSpace = name.indexOflast(" ");
    String firstName = "";
    String middleName = "";
    String lastName = "";

    if (startSpace >= 0)
    {
       firstName = name.substring(0, startSpace);
       if (endSpace > startSpace)
           middleName = name.substring(startSpace + 1, endSpace);
           lastName = name.substring(endSpace + 1, name.length());

    }

    System.out.println("First Name: " + firstName);
    System.out.println("Middle Name: " + middleName);
    System.out.println("Last Name: " + lastName);

}

```

}

Output: joe mark

```
First name: joe
Middle name: //this shouldn't print but because the user enter extra spaces after first name the spaces becomes the middle name.
Last name: mark 

```

Answer: 
This line:

```
int endSpace = name.indexOf(" ");

```

should be:

```
int endSpace = name.lastIndexOf(" ");

```
That's because `String.indexOf(char)` [returns the index of the first match](http://docs.oracle.com/javase/1.4.2/docs/api/java/lang/String.html#indexOf%28int%29), so `startSpace` and `endSpace` have the same value.

---

A more reliable way to achieve this would be to split on whitespace and count the number of resulting substrings. If there are two, there is no middle name; if there are three - the second item in the `String[]` is the middle name.

For example:

```
public static void extractName(String input) {
    String[] components = input.split("\\s+");

    if (components.length == 1) {
        System.out.println("The pseudonym is: " + components[0].trim());
    } else if (components.length == 2) {
        System.out.println(String.format(
                "The first name is: %s, last name is: %s ",
                components[0].trim(), components[1].trim()));
    } else if (components.length == 3) {
        System.out
                .println(String
                        .format("The first name is: %s, middle name is: %s, last name is: %s ",
                                components[0].trim(), components[1].trim(),
                                components[2].trim()));
    } else {
        System.out.println(String.format(
                "\"%s\"? I doubt it's your REAL name", input));
    }
}

public static void main(String[] args) {
    extractName("Prince");
    extractName("John Smith");
    extractName("John  M.  Smith");
    extractName("John  M. J. Smith");
}

```