Frage

Not really sure why jGrasp isn't accepting my toString equals method. Any help would be great, stuck at a stand still.

public class Rottweiler extends GuardDog {
    private String color = "Brown";

    public String toString() {
        String returnString = super.toString();
        returnString += String.format(Constant.FORMAT, "Color:", this.color);

        return toString;
    }
}
War es hilfreich?

Lösung

change toString to returnString like this:

public String toString() {
    String returnString = super.toString();
    returnString += String.format(Constant.FORMAT, "Color:", this.color);

    return returnString;
}

Andere Tipps

In the toString() method you are creating a String-object named returnString but you try to return an undefined object toString.

public String toString() {
    String returnString = ... // <--
    return toString; // <-- should be returnString
}

You're returning the wrong value in your toString method.

public String toString() {
    String returnString = super.toString();
    returnString += String.format(Constant.FORMAT, "Color:", this.color);
    return returnString; //this line is the correct one
}

The problem is your toString() method -

public String toString() {
  String returnString = super.toString();
  returnString += String.format(Constant.FORMAT, "Color:", this.color);
  return toString; // <-- Why toString? You want returnString.
}

I normally use a StringBuilder in my toString() method(s), so it might look like this -

public String toString() {
  StringBuilder sb = new StringBuilder(super.toString());
  sb.append(String.format(Constant.FORMAT, "Color:", this.color));
  return sb.toString();
}
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top