简体   繁体   中英

java : Error in Enum Declaration Saying Misplaced Constructors

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

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.

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

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;
    }
}

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:

}

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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM