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(" ");

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

```

These two will hold the same value. 

 **Another Option** 

Simple solution is to assume the name has three parts.

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

```