parse name no spaces?
<p>When a user enters more than one space, my program doesn't properly print the user's name out. For example, if the user enters their first name followed by 2 spaces and then their last name, my program assumes those extra spaces are the middle name and prints the middle name as the spaces and the last name as the second string entered, even though only two strings were entered. How can I improve this issue so that the extra space a user may enter doesn't count as a middle or last name?</p>

<pre><code>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 &gt;= 0)
    {
        firstName = name.substring(0, startSpace);
        if(endSpace &gt; 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);
}
</code></pre>

<p>Output: joe&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;mark</p>

<pre><code>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 
</code></pre>
