Exchange names with indexOf and substring and compareTo

I am having trouble understanding how to use the indexOf()

and substring()

and methods compareTo()

to switch the first name of people with their last name in an array.

+2


a source to share


3 answers


Let's say you have the following:

String[] names = new String[]{"Joe Bloggs", "Sam Sunday"};

      

You can use the following code to replace name and name:



for (int i=0; i < names.length; i++)
{
    String someName = names[i];
    int spaceBetweenFirstAndLastName = someName.indexOf(" ");
    //These next two lines of code may be off by a character
    //Grab the characters starting at position 0 in the String up to but not including
    //   the index of the space between first and last name
    String firstName = someName.substring(0, spaceBetweenFirstAndLastName);
    //Grab all the characters, but start at the character after the space and go 
    //   until the end of the string
    String lastName = someName.substring(spaceBetweenFirstAndLastName+1);
    //Now, swap the first and last name and put it back into the array
    names[i] = lastName + ", " + firstName;
}

      

The method compareTo

can now be used to sort names by comparing one name to another, now that the last name is the beginning of a string. Take a look at the api here and see if you can figure it out.

+2


a source


Yo can use the split method to split names into a String array, assuming they are separated by a space.

For example, consider a name:

String name = "Mario Freitas";

String[] array = name.split(" "); // the parameter is the string separator. In this case, is the space

for(String s : array){
    System.out.println(s);
}

      

this code will print each name on a different line (since the line has been separated)



Then you can compare the separated first and last name using the equals method.

Suppose you have 2 arrays of split strings, each with the same name Person.

public void compare names(String name1, String name2){
    String array1[] = name1.split(" ");
    String array2[] = name2.split(" ");

    if(array1[0].equals(array2[0])){
        System.out.println("First names are equal");
    }

    if(array1[1].equals(array2[1])){
        System.out.println("Second names are equal");
    }
}

      

+1


a source


You can use regular expressions, in particular String.replaceAll(String regex, String replacement)

, to reverse the order of the first and last name.

    String[] names = { "John A. Doe", "James Bond" };
    for (int i = 0; i < names.length; i++) {
        names[i] = names[i].replaceAll("(.*) (.*)", "$2, $1");
        System.out.println(names[i]);
    }

      

Prints:

Doe, John A.
Bond, James

      

However, if the only reason you change is because you later want to sort the last names, then you don't need to change at all. Just encapsulate your last name extraction code in a helper method (so that it can be tested, reused, etc.) and then use it in your usual java.util.Comparator

forjava.util.Arrays.sort

import java.util.*;

//...

static String lastName(String name) {
    return name.substring(name.lastIndexOf(' '));
}

//...

String[] names = { "John A. Doe", "James Bond" };       
Comparator<String> lastNameComparator = new Comparator<String>() {
    @Override public int compare(String name1, String name2) {
        return lastName(name1).compareTo(lastName(name2));
    }           
};
Arrays.sort(names, lastNameComparator);
for (String name : names) {
    System.out.println(name);
}

      

Prints:

James Bond
John A. Doe

      

Note that in both snippets, this is the last index of the space character, which is used to define the boundary between the first and last name.

0


a source







All Articles