-4

Take a String parameter that is a name, with exactly two names in the String and exactly one space between the two names.

For example, "Marley, Bob"

I need help completing this method: it returns a String with the same name, but in the form "Bob Marley"

public static String convertName(String name) 
{

}
Renea S.
  • 91
  • 1
  • 11
  • 4
    Another homework? What did you try? – bobbel Dec 12 '13 at 14:52
  • Many things, all of which did not work :) I'm not a slacker, I'm still learning. – Renea S. Dec 12 '13 at 14:57
  • 3
    @ReneaS. Then show it. Say if you did something like String.replace(","," "). Not only could someone explain WHAT to do to fix it but teach you WHY yours didn't work, giving you two benefits. Right now instead you have two poor submitted answers now and may have to fix theirs. – Lan Dec 12 '13 at 15:02

3 Answers3

2
String[] arr = name.split(", ");
return arr[1]+" "+arr[0];
Tim B
  • 40,716
  • 16
  • 83
  • 128
1

I will use a StringTokenizer and StringBuilder for this problem

public static String convertName(String name) 
{
    StringTokenizer tokenizer = new StringTokenizer(name, ", ");
    String lastName = tokenizer.nextToken();
    String firstName = tokenizer.nextToken();
    StringBuilder builder = new StringBuilder();
    builder.append("\nfirstName : " + firstName);
    builder.append("\nlastName : " + lastName);
    return builder.toString();                  
}
openmike
  • 272
  • 1
  • 9
  • I did some research on this, and it seems after JDK 1.4, the use of StringTokenizer to split String in Java is discouraged, and instead the String.split(...) method or use of java.util.regex package is encouraged. – Renea S. Dec 12 '13 at 15:02
  • You can do a perfomance test to see which one is faster, split() or tokenizer for one line and many lines; reference:http://stackoverflow.com/questions/736654/javas-scanner-vs-string-split-vs-stringtokenizer-which-should-i-use – openmike Dec 12 '13 at 15:06
  • nextElement() does not return a String…..http://docs.oracle.com/javase/7/docs/api/java/util/StringTokenizer.html#nextElement() @openmike If speed matters, one would just iterate over the String. – Lan Dec 12 '13 at 15:07
  • I made changes to tokenizer.nextToken(). Sorry about that. – openmike Dec 12 '13 at 15:09
  • Renea: performance test for a case where you have many lines of each full name and you have to call that method multiple times using an iterator or loop. – openmike Dec 12 '13 at 15:11
0

Apache Commons libraries provide great solutions unless you've specific requirements.

Have Commons Lang jar in your classpath. The following snippet may be the needed solution [doesn't look so elegant though].

StringUtils.replace(StringUtils.reverseDelimited(name, ','), ",", " ")

StringUtils.reverseDelimited(name, delimiter) - Reverses the order identified by delimiter. Ex: a,b,c becomes c,b,a

StringUtils.replace(name,searchString, replaceString) - Replaces all occurences of searchString with replaceString

Refer API - StringUtils - Commons Lang 2.6

ram
  • 747
  • 2
  • 11
  • 34