1

I cannot figure out how to change the order of a given name.

For example: Van-Dame Claud Result: Claud Dame-Van

I created this method, but it seems that is not correct, any suggestion or maybe a better method?

  public String NameInvers(){
String Name, Name = null, Name = null, NameFinal;
    int s1 = getName().indexOf(' ');
    int s2 = getName().indexOf('-');
    if(s1 != 0){
       Name1 = getName().substring(0, s1);
    if(s2 != 0)
    {
    Name2 = getName().substring(s1, s2);
    Name3 = getName().substring(s2);
    }
    NameFinal = Name3 + Name2 + Name1;
    }
    else
        NameFinal = getName();

    return NameFinal;
  }

Edit: In my Main method I have a number of names. The thing is that it says: throw new StringIndexOutOfBoundsException(subLen) and it won't show up.

Sandra
  • 77
  • 1
  • 7
  • "Seems not correct" -- can you be more specific? – Scott Hunter May 31 '15 at 00:00
  • I'm really trying to get a form of converting a String ("Van-Dame Claude") to ("Claude Dame-Van") but the method that I wrote doesn't work. – Sandra May 31 '15 at 00:07
  • 1
    read the doc for `indexOf`, I don't think it returns what you think it does (-1, for example) – njzk2 May 31 '15 at 00:12
  • Split it, keeping the separators. Then reverse the array. Done. See http://stackoverflow.com/questions/2206378/how-to-split-a-string-but-also-keep-the-delimiters. Also, some discourage this class, but I have no problem with it "StringTokenizer(String str, String delimiters, true);" That will keep the seperators, then just reverse the result and re-join it using StringJoiner. Done :) – The Coordinator May 31 '15 at 14:17

3 Answers3

3

First of all, you duplicate your variable Name declaration, three times. I suppose that you want to put:

String Name1, Name2 = null, Name3 = null, NameFinal;

instead of:

String Name, Name = null, Name = null, NameFinal;

Then, if you just have 2 names with a - in the middle and you want to change their order you can use split function to separate it, finding where - is. You can use it like this:

String string = "Van-Dame Claud";
String[] divide = string.split("-");
String Name1 = divide[0];
String Name2 = divide[1]; 
String result = Name2 + "-" + Name1;

EDIT: Name1 will be Van and Name2 will be Dame Claud so the result will be Dame Cloud-Van.

I expect it works for you!

Francisco Romero
  • 12,787
  • 22
  • 92
  • 167
  • This works perfect, the only thing is that in my array I have names that are just "Victor Hugo" and then I'm supposed to put "Hugo Victor" Thanks! – Sandra May 31 '15 at 01:24
3

Here you go:

public static String reverseName (String name) {

    name = name.trim();

    StringBuilder reversedNameBuilder = new StringBuilder();
    StringBuilder subNameBuilder = new StringBuilder();

    for (int i = 0; i < name.length(); i++) {

        char currentChar = name.charAt(i);

        if (currentChar != ' ' && currentChar != '-') {
            subNameBuilder.append(currentChar);
        } else {
            reversedNameBuilder.insert(0, currentChar + subNameBuilder.toString());
            subNameBuilder.setLength(0);
        }

    }

    return reversedNameBuilder.insert(0, subNameBuilder.toString()).toString();

}

Test:

public static void main(String[] args) {

    printTest("Van-Dame Claud");
    printTest("Victor Hugo");
    printTest("Anna");
    printTest("");

}

private static void printTest(String S) {

    System.out.printf("Reverse name for %s: %s\n", S, reverseName(S));

}

Output:

Reverse name for Van-Dame Claud: Claud Dame-Van
Reverse name for Victor Hugo: Hugo Victor
Reverse name for Anna: Anna
Reverse name for : 
Luca Fagioli
  • 12,722
  • 5
  • 59
  • 57
  • 1
    Thanks a lot Luca! This works perfect even for larger names. I know that there are several ways for reaching my goal and this is one I have definitely never thought of. – Sandra May 31 '15 at 14:32
1

You want to have a method to convert all names or just the the names in form of Name-Name Name? I'm assuming you want a method for all names. Since if you just want to reverse names in this specific form, there is no meaning for you to check value of s1 and s2, you know s1 and s2 must be a positive number. If you check because you want to do error checking, then you should throw an exception if the name is not in the form you want. If you want to do it for all names, below is the pseudocode, you can implement it yourself (your code contains several errors, you are using Name1, Name2, Name3, but you never declared them and duplicate variable Name declaration):

    string finalName = ""
    string [] strs1 = name split by " "
    foreach str1 in strs1
      string [] strs2 = str1 split by "-"
      foreach str2 in strs2
        if str2.first?
          finalName = str2 + " " + finalName
        else
          finalName = str2 + "-" + finalName
        end
      end
   end
   return finalName.substring 0, finalName.length-1 //remove the space at the end

I think this would work. Hope you find it useful.

Xing Yan
  • 19
  • 4