2

I may get result of any of the type , so i am defining enum this way

public enum Result 
     {
        1, 2,3, 4,5, 6,7, 8
     }


String resultvalue = calculateResult();

    switch (Result .valueOf(resultvalue ))
          {

          }

But i am geting error at the Enum Declaration itself saying Mispalced Constructors .

Could anybody please help me

5 Answers 5

7

Those aren't valid identifiers for enum values, basically. You'll need to prefix them with a letter or _. You'll also need to make the identifiers unique - currently you've got 0010 four times...

Once you've sorted that out, the rest should probably be okay - but if you have any more problems, please post a short but complete program, rather than snippets.

Sign up to request clarification or add additional context in comments.

Comments

2

0001 is not a valid Java identifier. A Java Identifier must not start with a digit.

Comments

0

Although I don't understand what you want to achieve and why you have duplicates. Something like that (maybe using an int instead of String) should work.

public enum Result {
    One( "0001"),
    Two( "0010")
    ...

    private String val;

    private Result(String val) {
        this.val = val;
    }
}

Comments

0

I an not sure why calcuate result would return a String. I would return a int here but...

String resultvalue = calculateResult();
switch (Integer.parseInt(resultvalue)) {
   case 0b0001:

   case 0b0010:

   case 0b0110:

   case 0b1010:

   case 0b1100:

}

Comments

0

What is it that you are trying to achieve? If you need to:

  1. parse an integer from a string, then
  2. check that it's from a certain set of values, and finally
  3. switch on its value,

then you don't need an enum. Just do it with Integer.parseInt(), Set.contains(), and switch.

Comments

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.