-6

So I made a name class, and am using an array to store first and last names with the split function delimiting " ". Edit: Sorry its part of bigger project, it would be pointless to copy all of the code. I Have a Person Class thats composed of Name class. The person class utilizes the getStatement below, with println to print it out.

public Name(String name){
    String [] nameArr = name.split(" ");
    for (int i = 0; i < nameArr.length; i++){
        if (i == 0)
            firstName = nameArr[i];
        else{
            if(nameArr[i] != null){
                lastName += nameArr[i];
            }                    
        }
    }
}

public String getName(){
        return (name.getLast() + " " +  name.getFirst());
    }

The out put for a give name, Joe Shmoe, Comes out to nullShome Joe... I cant figure out why =[

6
  • 2
    There is no single line printing anything in the code you posted. So how could we know why you get that output? And we don't know the input, either. Commented Jul 4, 2016 at 16:35
  • Please upload the full code of the class so that we can help you. Commented Jul 4, 2016 at 16:37
  • ahh, I see, but some of the names on the file im readiing from have multiple last names Commented Jul 4, 2016 at 16:39
  • thanks friend, much appreciated. Commented Jul 4, 2016 at 16:41
  • Related: Why does a null value appear in string output? Commented Jul 4, 2016 at 16:46

2 Answers 2

3

Just initialize the lastName property to an empty string in front of the loop:

lastName = "";
for (int i = 0; i < nameArr.length; i++){ 
    ...

Otherwise, lastName is initially null, and the statement lastName += nameArr[i]; results in lastName being set to null<LastName>.


Actually, you can avoid the loop altogether by using the String.split() method with the limit parameter set to 2:

public Name(String name){
    String[] nameArr = name.split(" ", 2);
    firstName = nameArr.length > 0 ? nameArr[0] : "";
    lastName = nameArr.length > 1 ? nameArr[1] : "";
}

Another general hint: when building up a string by concatenations in a loop, it's usually a good idea to use a StringBuilder, to avoid performance penalty.

Sign up to request clarification or add additional context in comments.

2 Comments

wow beautiful answer thanks for links
...or if you want to build string with delimiter (like space, comma or other) between tokens since Java 8 we can also use StringJoiner.
1

When you append a String which is null you get null in it. e.g.

String a = null;
a += " hello"; // a = "null hello";

In your case you have a lastName which was null but is now added a name after the null.

The best way to see what is happening is to step through the code in your debugger in your IDE. This will show you what each line does.

What you want is

String[] nameArr = name.split(" ", 2); // 1 or 2 elements.
firstName = nameArr[0]; // always at least one element.
lastName = nameArr.length > 1 ? nameArr[1] : "";

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.