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

4

5 回答 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.

于 2012-04-13T13:18:25.257 回答
2

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

于 2012-04-13T13:18:46.110 回答
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;
    }
}
于 2012-04-13T13:21:26.153 回答
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:

}
于 2012-04-13T13:23:54.123 回答
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.

于 2012-04-13T13:54:27.197 回答