0

I'm trying to create a a method that combines the first and last name variables to create a fullName variable but I keep getting null whenever instead of the full name? I'm not getting any actual errors so I don't know where it's going wrong?

  @Override

 public String toString() {
    return String.format("%-22s  %.2f  %-4s  %s", fullName, gpa, major, year);

  }

  public void setfirstName(String firstName) {
    this.firstName = firstName;
  }

  public String getfirstName() {
    return firstName;

  }

  public void setlastName(String lastName) {
    this.lastName = lastName;
  }

  public String getlastName() {
    return lastName;

  }
public void setfullName(String fullName) {
    fullName = (lastName + ", " + firstName);
  }

  public String getfullName() {
    return fullName;

1 Answer 1

1

Here, your fullname is a local variable inside the following method.

public void setfullName(String fullName) {
    fullName = (lastName + ", " + firstName);
}

Just make it an attribute of the object and user this.fullname to assign the full name.Also there's no point in passing parameters to setFullName() method, since what you are doing is concatenating first name and the last name.

public void setfullName() {
    this.fullName = (lastName + ", " + firstName);
}

Therefore instead of using a setFullName() method, you can only use getFullName() method as follows.

public String getfullName() {
    return lastName + ", " + firstName;
}
Sign up to request clarification or add additional context in comments.

9 Comments

I'm still getting 'null'. I had tried just the getfullName method initially but it gave me null which is why i assumed I needed the setter, any idea why it's doing it?
because you don't have an attribute called firstName in the class. Even though there is, in the setFullName method, you have another variable in the same name,which is local. You are assigning to that local variable. TO assign the class variable, use this.fullName.
I'm sorry but I'm confused, where exactly am I suppose to use this.fullname? Within one of the firstName methods or within the toString?
@HiImIsabella It should be inside of setfullName.
@PufanJiang I thought I was suppose to use only the getFullName and the set method?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.