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: 
As an alternative to using the indexOf take a look at the [String.split](http://docs.oracle.com/javase/6/docs/api/java/lang/String.html#split%28java.lang.String%29) method.

```
String[] names = name.split(" ");

```

Then you can write your code depending on how many names they typed, so they could type just their first name; first and last; or first, middle, and last.
`startspace` and `endspace` have the same value. Therefore, 

```
 middleName = name.substring(startSpace + 1, endSpace);

```

will give an empty `String`.

```
lastName = name.substring(endSpace + 1, name.length());

```

will give everything after the first space