java - return String with switch case

2.4k Views Asked by At

i tried to use an enum for my first time. For some Tests i want to override the toString method of my enum and return a String with the chosen enum.

Heres my code so far:

@Override
public String toString()
{
    return "Fahrzeuge{" +
            switch(this)
            {
                case MOTORAD: "1"; break;
                case LKW: "2"; break;
                case PKW: "3"; break;
                case FAHRRAD: "4"; break;
            }
            +
            "typ:" + this.typ +
            ", ps:" + this.ps +
            ", reifen:" + this.reifen +
            ", gewicht:" + this.gewicht +
            "}";
}

IntelliJ underline my cases and tell me the following: "Not a Statement" => I guess this makes sense, if it's not allowed to build a String with a switch - case.

So far so good, but it seem's to be impossible to return a String which is build through a switch case, or did i make a mistake at my return? Any other options to return the chosen enum? I could add an attribute that hold my chosen enums name, but i though i could do this a bit simpler.

Thanks for help

2

There are 2 best solutions below

1
On BEST ANSWER

I think you really don't need the switch Statement because the superclass of the enum already knows the name of your "type":

@Override
public String toString()
{
    return "Fahrzeuge: " + super.toString() +
            ", ps:" + this.ps +
            ", reifen:" + this.reifen +
            ", gewicht:" + this.gewicht;
}

Simply call the toString() method of the super class and you get the string value of your currently selected enum-type. You even can delete your type string.

6
On

It's possible to return the value of a switch statement starting with Java 12, as per JEP 325. Check your version of Java, if it's anything less than 12 then you can't use switch like that and you'll have to resort to first saving the intended value in a local variable. My point is, if your version of java is older than 12, then you have to do this:

String num = "";
switch (this)
{
    case MOTORAD:
        num = "1";
        break;
    case LKW:
        num = "2";
        break;
    case PKW:
        num = "3";
        break;
    case FAHRRAD:
        num = "4";
        break;
}

return "Fahrzeuge{" + num +
            "typ:" + this.typ +
            ", ps:" + this.ps +
            ", reifen:" + this.reifen +
            ", gewicht:" + this.gewicht +
            "}";

But if you have Java 12 (or above) installed, then you can do this (notice the different syntax!):

return "Fahrzeuge{" +
            switch (this)
            {
                case MOTORAD -> "1";
                case LKW     -> "2";
                case PKW     -> "3";
                case FAHRRAD -> "4";
            }
            + "typ:" + this.typ +
            ", ps:" + this.ps +
            ", reifen:" + this.reifen +
            ", gewicht:" + this.gewicht +
            "}";

And do notice that if the numbers correspond to the order in which the enum values were declared, you can simply use ordinal():

return "Fahrzeuge{" + this.ordinal() +
            "typ:" + this.typ +
            ", ps:" + this.ps +
            ", reifen:" + this.reifen +
            ", gewicht:" + this.gewicht +
            "}";