-1
String nameStr = "george raymond richard martin";

String formattedName = " ", firstName = " ", middleName = " ", lastName = " ", middleInitial = " ";

int index = 0;



if (nameStr.indexOf(" ") == -1) { //for one name case

    formattedName = nameStr.substring(0, 1).toUpperCase().substring(1).toLowerCase();

}

if (nameStr.indexOf(" ") != -1) {

    String nameParts[] = nameStr.split(" ");

    int N = nameParts.length;

    if (N == 2) { //if there are just two names

        firstName = nameParts[0];

        lastName = nameParts[N - 1];

        firstName = firstName.substring(0, 1).toUpperCase().substring(1).toLowerCase();

        lastName = lastName.substring(0, 1).toUpperCase().substring(1).toLowerCase();

        formattedName = lastName + ", " + firstName;

    } else { //for any amount of names

        for (index = 1; index <= N - 2; index++) {

            middleName = nameParts[index];

            firstName = nameParts[0];

            lastName = nameParts[N - 1];

            middleInitial = middleName.substring(0, 1).toUpperCase() + ".";

            formattedName = lastName + ", " + firstName + middleInitial;


        }
    }
}

System.out.println(formattedName);

this is what I have right now, and I feel like my logic is on the right track but it's not printing anything out properly. It doesn't print for the one word case, and for the two word case it just prints out the comma. The case for any amount of middle names works, but only for three words, and if there is more than one middle name it just truncates it to the last middle name. I have no idea what the issue is, I just feel like i need someone else to look at it. Thanks :)

baao
  • 71,625
  • 17
  • 143
  • 203
  • 3
    Don't forget to test it with Picasso's full name: Pablo Diego José Francisco de Paula Juan Nepomuceno María de los Remedios Cipriano de la Santísima Trinidad Ruiz y Picasso – Elliott Frisch Sep 16 '16 at 21:35
  • If you have no idea what the issue is, then you have more debugging to do before posting. As a side comment, using regex would make this much easier. – 4castle Sep 16 '16 at 21:36
  • 1
    Look at this line very carefully: `.substring(0,1).toUpperCase().substring(1)` the substring after index `1` of a length `1` substring will always be an empty string. – 4castle Sep 16 '16 at 21:43
  • What about two first names, like Billy Bob Thornton? Or about two last names, like Maria von Trapp? – rajah9 Sep 16 '16 at 21:44
  • Use the Scanner class. Set the delimiter to whitespace, iterate through, put the strings into a list then pick the first and the last one, then the others. Uppercase the first chars and concatenate the list of strings into a single one. – Tacsiazuma Sep 16 '16 at 21:49
  • Possible duplicate of [What is a debugger and how can it help me diagnose problems?](http://stackoverflow.com/questions/25385173/what-is-a-debugger-and-how-can-it-help-me-diagnose-problems) – Raedwald Sep 17 '16 at 08:25

4 Answers4

3

I think what you need is the String#split(String) method. If the full name is a String of space-separated words, split will make quick work of your problem.

I assume that you want "George Raymond Richard Martin" to come out as "George R. R. Martin" because... well, that's what he goes by. Otherwise, you haven't really told us what the specs are, but this is how you'd accomplish that.

String toShortenedName(String fullName) {
    assert(fullName != null); // :P

    // Split the fullName where there are 1-or-more whitespaces.
    String[] nameParts = fullName.trim().split("\\s+");
    // We'll be building our return value with this.
    StringBuilder nameBuilder = new StringBuilder();

    for (int i = 0; i < nameParts.length; i++) {
        if (i > 0) {
            // Not the first iteration; add a space to what came before.
            nameBuilder.append(' ');
        }

        String part = nameParts[i];
        if (i == 0 || i == nameParts.length - 1) {
            // first or last name
            nameBuilder.append(part);
        } else {
            char middleInitial = part.charAt(0);
            nameBuilder.append(String.format("%c.", middleInitial));
        }
    }

    return nameBuilder.toString();
}
nasukkin
  • 2,460
  • 1
  • 12
  • 19
0

formattedName=nameStr.substring(0,1).toUpperCase().substring(1).toLowerCase()

So, first you take the first letter from string nameStr and convert it to upperCase. What you do next (substring(1)) is actually substringing that single letter you took, not the whole previous string. You need to first convert that single letter to upperCase, and then in another line append the rest of the original string to your new string. Something like this:

formattedName = nameStr.substring(0,1).toUpperCase();

formattedName += nameStr.substring(1).toLowerCase();
Shadov
  • 5,421
  • 2
  • 19
  • 38
0

This is your code slightly updated :

// TODO Auto-generated method stub
  //String nameStr = "george raymond richard martin";
  String nameStr = "Pablo Diego José Francisco de Paula Juan Nepomuceno María de los Remedios Cipriano de la Santísima Trinidad Ruiz y Picasso";


    String formattedName = " ", firstName = " ", middleName = " ", lastName = " ", middleInitial = " ";

    int index=0;



    if(nameStr.indexOf(" ") == -1){ //for one name case

        formattedName = nameStr.substring(0,1).toUpperCase().substring(1).toLowerCase();

    }

    if(nameStr.indexOf(" ") != -1){ 

        String nameParts [] = nameStr.split(" ");

        int N = nameParts.length;

        firstName = nameParts[0];

        lastName = nameParts[N-1];



            if(N == 2){ //if there are just two names

                firstName = nameParts[0];

                lastName = nameParts[N-1];

                firstName = firstName.substring(0,1).toUpperCase().substring(1).toLowerCase();

                lastName = lastName.substring(0,1).toUpperCase().substring(1).toLowerCase();

                formattedName = lastName + ", " + firstName;

                    }else{  //for any amount of names

                        for(index = 1; index<=N-2; index++){

                            middleName = nameParts[index];

                            middleInitial = middleInitial + middleName.substring(0, 1).toUpperCase()+ ".";





                    }
                }
            }

            formattedName = lastName + ", " + firstName + middleInitial;
            System.out.println(formattedName);
dodger
  • 245
  • 1
  • 3
  • 17
0

I send my solution. Maybe help you. Output: Martin, George R.R.

    String nameStr = "george raymond richard  martin";
    String  firstName = " ", lastName = " ", middleInitial = " ";
    StringBuilder formattedName=new StringBuilder();
    StringBuilder nb=new StringBuilder();


    String[] splitNames=(nameStr.split("\\s+"));

    for(int i=0;i<splitNames.length;i++){
        if(i>0 && i<splitNames.length-1) {
                splitNames[i]=splitNames[i].substring(0,1).toUpperCase()+".";
                nb.append(splitNames[i]);
        }else{

            splitNames[i]=splitNames[i].substring(0,1).toUpperCase()+splitNames[i].substring(1).toLowerCase();
        }

    }
    firstName=splitNames[0];
    lastName=splitNames[splitNames.length-1];

    formattedName.append(lastName);
    formattedName.append(", ");
    formattedName.append(firstName+" ");
    formattedName.append(nb);



    System.out.println(formattedName.toString());