views:

166

answers:

3

I am having trouble understanding how to use the indexOf() and substring() and compareTo() methods to flip people's first name with their last name in an array.

+1  A: 

Yo can use the split method to separate the names into a String array, considering that they are separed by a space.

For example, lets 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 in a different line (as the String was separated)

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

Let's suppose you have 2 array of Strings, obtained by split method and each one with one different Person name.

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");
    }
}
marionmaiden
all the names are already stored in a string array i need to flip them and put them back in the same array
dalton
You can flip the names using the split method. Just create another string filling it with the values of the array fliped. Then you have the fliped name. Ex.: String s = array1[1] + " " + array1[0];
marionmaiden
+2  A: 

Let's say you have the following:

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

You can use the following code to swap the last name and first 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 string compareTo method can now be used to sort the names by comparing one name against another, now that the last name is the start of the string. Have a look at the api here and see if you can figure it out.

Chris Knight
thank you that helps out alot
dalton
A: 

You can use regular expressions, specifically String.replaceAll(String regex, String replacement) to reorder the first name 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]);
    }

This prints:

Doe, John A.
Bond, James

However, if the only reason why you're swapping is because you later want to sort on last names, then you actually don't need to swap at all. Just encapsulate the last name extracting code into a helper method (so it's testable, reusable, etc), and then use it in your custom java.util.Comparator for java.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);
}

This prints:

James Bond
John A. Doe

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

polygenelubricants