6

Ok, so it's the first time I am posting out here, so bear with me.

I have a name in the format of "Smith, Bob I" and I need to switch this string around to read "Bob I. Smith". Any ideas on how to go about doing this?

This is one way that I've tried, and while it does get the job done, It looks pretty sloppy.

public static void main(String[] args) {
        String s = "Smith, Bob I.", r = "";
        String[] names;

        for(int i =0; i < s.length(); i++){
            if(s.indexOf(',') != -1){
                if(s.charAt(i) != ',')
                    r += s.charAt(i);
            }

        }
        names = r.split(" ");
        for(int i = 0; i < names.length; i++){
        }
        System.out.println(names[1] +" " + names[2] + " " + names[0]);


    }
Venki
  • 1,419
  • 5
  • 28
  • 38
Lambda
  • 151
  • 1
  • 10
  • Your algorithm will probably depend a bit on what sort of rules about input you have, as this may require additional steps to handle special cases or allow for various shortcuts because the data is consistent. Eg: is there always a middle initial present or will there sometimes be no middle name, or a full middle name. What about titles like II or Jr? Do all the names have a first AND last? Etc. – Jessica Brown May 10 '12 at 20:25
  • Okay so this is a tool that collects the names as they are brought in from either unstructured texts or they are collected by scraping the web. They come in in all kinds of formats i.e. "Bob I. Smith", "Smith, Bob I", "Bob Smith", "Smith Bob". I need to develop a way to make all the names a uniformed format, which,for now, is either 'First M. Last' or 'Last, First M' – Lambda May 10 '12 at 22:20
  • @Ninja-neer well, without knowing the format of the texts that can be quite hard. How would you tell the difference between "Smith Bob" and "Bob Smith" (i.e. what is the first name and what is the last name) if you don't know what "Bob" and "Smith" mean (which the computer doesn't). – Thomas May 11 '12 at 06:20
  • The way I did it was to check to see if the string contained a comma. If it did then most likely it is in the "Last, First" format. But I haven't come with a solution for if the comma is not there – Lambda May 11 '12 at 12:48

4 Answers4

10

If the name is always <last name>, <firstname>, try this:

String name = "Smith, Bob I.".replaceAll( "(.*),\\s+(.*)", "$2 $1" );

This will collect Smith into group 1 and Bob I. into group 2, which then are accessed as $1 and $2 in the replacement string. Due to the (.*) groups in the expression the entire string matches and will be replaced completely by the replacement, which is just the 2 groups swapped and separated by a space character.

Thomas
  • 87,414
  • 12
  • 119
  • 157
  • @userunknown as far as I can tell from the OP that's a requirement: `Smith, Bob I` -> `Bob I. Smith` - although I admit that I didn't add the dot, as I'm unsure whether that's a typo (in the OP's code the dot is present) or actually a requirement - should be easy to add though. – Thomas May 10 '12 at 20:35
  • @userunknown or you have a pixel error: `I.` + pixel error could look like `I,` :P – Thomas May 10 '12 at 20:39
  • Some people look at a problem and say, "I know, I'll use a regex." Now their problem is solved and they happily move to the next problem. +1 – yshavit May 10 '12 at 20:46
  • Here is what I am using it for. The program retrieves names from unstructured text and displays them in the format they're received in. I want to make a tool that displays them in a user specified format of either: "First M. Last" or "Last, First M." – Lambda May 11 '12 at 01:03
5
    String[] names = "Smith, Bob I.".split("[, ]+");
    System.out.println(names[1] + " " + names[2] + " " + names[0]);
Christopher Oezbek
  • 23,994
  • 6
  • 61
  • 85
4
final String[] s = "Smith, Bob I.".split(",");
System.out.println(String.format("%s %s", s[1].trim(), s[0]));
Marko Topolnik
  • 195,646
  • 29
  • 319
  • 436
3
String s = "Smith, Bob I.";
String result = s.substring(s.indexOf(" ")).trim() + " "
            + s.substring(0, s.indexOf(","));
isvforall
  • 8,768
  • 6
  • 35
  • 50