Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Switch on Enum when strings contain dash

I would like to use enum as a way of switching over strings, however java complains as my string contains "-". As seen in the code below where IC19-01 and IC19-02 contain "-".

public class CMain {
    public enum Model {
        IC19-01, IC19-02
    }

    public static void main(String[] args){
        String st = "IC19-01"; 
        switch (Model.valueOf(st)) {
            case IC19-01: 
                System.out.println("Case IC19-01");
                break;
        }
    }
}

What can i do for this?

like image 397
C graphics Avatar asked Feb 17 '23 00:02

C graphics


1 Answers

This is not possible with Java, because each item has to be a valid identifier (and valid Java identifiers may not contain dashes).

like image 179
joan Avatar answered Feb 27 '23 15:02

joan