• Register
0 votes
1.6k views

Problem :

I am much more familiar with the enums in C# but it seems like enums in the Java is a quite different.

Currently I am trying to use the switch statement @ enums in my below code but I always face the below error no matter what I am doing.

The error I receive is as below:

The qualified case label MySomeClass.MyAnotherClass.MyEnum.MYVALUE_A must be replaced with the unqualified enum constant MYVALUE_A

I am able to understand the error but I am unable to write the MYVALUE_A as my enum is located in the another sub-class. Is there any way to solve my problem?

//My Main Class
public class MySomeClass {
    //My Sub-Class
    public static class MyAnotherClass {
        public enum MyEnum {
            MYVALUE_A, MYVALUE_B
        }    
        public MyEnum myEnum;
    }
    public void mysomeMethod() { 
        MyEnum myenumExample //...
        switch (myenumExample) {
            case MyAnotherClass.MyEnum.MYVALUE_A: { <-- facing error on this line
                //..
                break;
            }
        }
    }
}
6 5 3
7,540 points

Please log in or register to answer this question.

1 Answer

0 votes

Solution :

The Java infers automatically any type of the elements in the case, so your labels must be unqualified as shown in below example :

int j;
switch(j) {
   case 9: // <- Here integer is expected
}
MyEnum mye;
switch (mye) {
   case MYVALUE_A: // <- Here an element of the enumeration is expected
}
9 7 4
38,600 points

Related questions

0 votes
1 answer 15 views
15 views
How I will solve this error?
asked Dec 19, 2020 TeamScript 13.5k points
0 votes
1 answer 118 views
118 views
Problem: Hello! I am a new learner. I recently started programming in C#. I learned the control statements in which I came across if, if-else, and switch statements. I tried the following program to have a better understanding of switch statement: using System; class HelloWorld ... label `case default:' I do not know what I have done wrong. Can anybody help me in figuring out what is wrong here?
asked Jan 4 Code Learner 9.8k points
0 votes
1 answer 11 views
11 views
Problem: Any possible solution will be admitted with thanks: cannot jump from switch statement to this case label c++ .
asked Mar 14 Wafa Abu Yousef 6.1k points
0 votes
1 answer 25 views
25 views
Problem: The problem is that variables declared in one case are still visible in the subsequent cases unless an explicit { } block is used, but they will not be initialized because the initialization code belongs to another case.
asked Dec 15, 2020 Mashhoodch 13k points
3 votes
2 answers 1.5K views
1.5K views
Problem: Getting an error while I was working with a switch statement in c++. I do have a 200 lines code that I can&rsquo;t share with you for obvious reason. I am sharing the switch-statement block that produces the error in my program. The error I am getting is: cannot jump ... { case 1: int i = 42; dostuff(i); break; case 2: dostuff(i*2); } Any possible solution will be admitted with thanks.
asked Mar 25, 2020 Gavin 15.3k points
0 votes
1 answer 3 views
3 views
Problem: I am Making a calculator using a switch case. The result I get : Enter a number : 5 Enter another number : 4 What do you want to perform on these numbers? You have entered a wrong action, please try again
asked Mar 23 ummesalma 29.2k points
0 votes
1 answer 69 views
69 views
Problem Hey I Am facing an error in my calculator code. I don&rsquo;t know how to resolve this error. Will you please help me to resolve this or any other help? Here&rsquo;s my code Program: #include <iostream> #include <cmath> using namespace std;          void display_menu();  int get_menu_choice(); void ... b)   {    return (a + b);   }  int subtract(int a, int b)   {     return (a - b);   }
asked Dec 21, 2020 chris jordan 2.4k points
0 votes
1 answer 3 views
3 views
Problem: Can anyone assist me with a Java program that prints a number in words using switch case?
asked Apr 17 jamuna1 30.2k points
0 votes
1 answer 2 views
2 views
Problem: Could you please recommend a C program that prints a number in words using switch case?
asked Apr 17 jamuna1 30.2k points
0 votes
1 answer 5 views
5 views
Problem: i need fix this problem who know about python please help how to use switch case in python
asked Mar 16 Rohit kr 17.5k points