Swapping names using indexOf and substring and compareTo?

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 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 namesi = 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.

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 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 namesi = 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.

Thank you that helps out alot – dalton Apr 29 '10 at 20:00.

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(array10. Equals(array20)){ System.out.

Println("First names are equal"); } if(array11. Equals(array21)){ System.out. Println("Second names are equal"); } }.

All the names are already stored in a string array I need to flip them and put them back in the same array – dalton Apr 29 '10 at 19:30 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 = array11 + " " + array10; – marionmaiden Apr 29 '10 at 19:35.

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 lastNameComparator = new Comparator() { @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.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions