0

I declared enum as an input to the switch statement since it doesn't allow String values.

public enum names{VALUE1, VALUE2}

I have a List of values (VALUE1, VALUE2, VALUE3)

for(int i=0; i<list.size();i++)
{
    names n=new names(list.get(i).trim());
    switch(n) {
        case VALUE1:
            System.out.println("1");
            break;
        case VALUE2:
            System.out.println("2");
            break:
        default:
            System.out.println("Nothing to print");
    }
}

While executing the above-mentioned code, I am getting No Enum constant Class.data.VALUE3. error. Please help me resolve this error.

Orsu Suni
  • 131
  • 1
  • 6
  • 12
  • 1
    I have a `List` of values `(VALUE1, VALUE2, VALUE3)` if these are of type data, what else did you expect? – Stultuske Nov 27 '17 at 09:01
  • 2
    `data` isn't a type you can switch over. You can only switch on integers, strings and enums. If you can write `new data(...)`, it's not one of those. – Andy Turner Nov 27 '17 at 09:02
  • @AndyTurner as long as he uses something like: case data.VALUE1:, why not? – Stultuske Nov 27 '17 at 09:04
  • @Stultuske because `d` isn't an enum type if you can create an instance of it, it's not a string, and it's not an integer. You just can't switch on this. If it were `data d = /* some method returning a value from data */`, fine - but it's not that. – Andy Turner Nov 27 '17 at 09:05
  • @AndyTurner you can switch over enums, as long as you do it correctly. just take a look at this thread: https://stackoverflow.com/questions/6391777/switch-on-enum-in-java – Stultuske Nov 27 '17 at 09:06
  • 1
    @Stultuske for the third time: *it isn't an enum type if you can create an instance of it*. – Andy Turner Nov 27 '17 at 09:07
  • it's not practical, but it is possible, yes, even for enum types. – Stultuske Nov 27 '17 at 09:08
  • @Orsu "as an input to the switch statement since it doesn't allow String values." which version of Java are you using? Switching on strings has been allowed since Java 7. – Andy Turner Nov 27 '17 at 09:12
  • @Andy I am using java 7 – Orsu Suni Nov 27 '17 at 09:14
  • 2
    @OrsuSuni then switching on strings is allowed. Why do you think you can't use it? – Andy Turner Nov 27 '17 at 09:15

1 Answers1

1

You can use names.valueOf(), but it will throw an exception if the name is not found:

for(int i=0; i<list.size();i++)
{
    try {
        names n=names.valueOf(list.get(i).trim());
        switch(n) {
            case VALUE1:
                System.out.println("1");
                break;
            case VALUE2:
                System.out.println("2");
                break:
            default:
                System.out.println("Nothing to print");
        }
    } catch (IllegalArgumentException e) {
        System.out.println("Nothing to print");
    }
}

Otherwise, you need a loop:

private static names find(String name) {
    for (names n: names.values()) {
        if (n.name().equals(name)) {
            return n;
        }
    }
    return null;
}

but you would need to check for null before the switch:

for(int i=0; i<list.size();i++)
{
    names n=find(list.get(i).trim());
    if (n != null) {
        switch(n) {
            case VALUE1:
                System.out.println("1");
                break;
            case VALUE2:
                System.out.println("2");
                break:
            default:
                System.out.println("Nothing to print");
        }
    } else {
        System.out.println("Nothing to print");
    }
}
Maurice Perry
  • 9,261
  • 2
  • 12
  • 24