I'm making a program that takes the last letter and second to last letter of a name and displays a formatted version in front of the name. I'm passing an array to hold all the names and it seems to work properly, but it displays null before each output. Why is this happening?

Don't mind the slop
Class:

public class EmployeeNames {
    public static String[] convertName(String[] lastNames){
        String lastLetter;
        String secondToLast;
        String[] putTogether = new String[10];

        for(int i = 0; i < putTogether.length; i++){
            lastLetter = (lastNames[i].substring(lastNames[i].length() - 1)).toUpperCase();
            secondToLast = (lastNames[i].substring(lastNames[i].length() - 2, lastNames[i].length() - 1)).toUpperCase();

            putTogether[i] += lastLetter + ". " + secondToLast + ". " + lastNames[i];
        }

        return putTogether;
    }
}

And don't mind the names
Driver:

public class EmployeeNamesTester {
    public static void main(String args[]){
        EmployeeNames prog = new EmployeeNames();
        String[] lastNames = {"Smith", "Jackson", "Sahara", "Adventist", "Maxwell", 
                              "Jainism", "Port", "Pritzker", "Scholomo", "Cobin"};

        for(int i = 0; i < 10; i++){
            String[] format = prog.convertName(lastNames);
            System.out.println(format[i]);
        }
    }
}

Current output:

nullH. T. Smith
nullN. O. Jackson
nullA. R. Sahara
nullT. S. Adventist
nullL. L. Maxwell
nullM. S. Jainism
nullT. R. Port
nullR. E. Pritzker
nullO. M. Scholomo
nullN. I. Cobin

that is because you do this:

putTogether[i] += lastLetter + ". " + secondToLast + ". " + lastNames[i];

Saying: the new value is the existing value, concatenated with wath follows after the +=

The existing value at that point, is null.

so, change that line to:

putTogether[i] = lastLetter + ". " + secondToLast + ". " + lastNames[i];

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.