user2264244
user2264244

Reputation: 65

parse full name?

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 

Upvotes: 1

Views: 5753

Answers (6)

kryger
kryger

Reputation: 13181

That's because String.indexOf(char) returns the index of the first match, 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");
}

Upvotes: 5

Hiro2k
Hiro2k

Reputation: 5587

As an alternative to using the indexOf take a look at the String.split 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.

Upvotes: 5

DeltaLima
DeltaLima

Reputation: 5944

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

Upvotes: 1

Rob Watts
Rob Watts

Reputation: 7146

The problem is with these two lines:

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

You want

int endSpace = name.lastIndefOf(" ");

Also, you're missing brackets for the nested if statement.

Upvotes: 2

Bruno Lowagie
Bruno Lowagie

Reputation: 77528

This line:

int endSpace = name.indexOf(" ");

should be:

int endSpace = name.lastIndexOf(" ");

Upvotes: 5

christopher
christopher

Reputation: 27336

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

Upvotes: 2

Related Questions