I am currently using IntelliJ and an error that says "Duplicate label '2'" appears when I am placing a case for pressing the CANCEL option in my menu.
import javax.swing.*;
public class Main {
public static void main(String[] args){
int choice=0;
Object menu= "1. Name Constructor\n" +
"2. Pretty Printing of text\n" +
"3. FLAMES\n" +
"4. Your Superhero name!\n" +
"5. return to the main menu\n";
do {
choice = Integer.parseInt(JOptionPane.showInputDialog(null,
"S T R I N G M A N I P U L A T I O N M E N U\n" +
menu, "Menu", 1));
switch (choice) {
case 1:
break;
case 2:
break;
case 3:
break;
case 4:
break;
case 5:
break;
case JOptionPane.CANCEL_OPTION:
break;
default:
JOptionPane.showMessageDialog(null,"Enter a valid choice.","Error",1);
break;
}
}while(choice!=5);
}
}
That is happening because you can't define two cases with the same value in the switch statement.
If you take a look inside the JOptionPane, you'll see that the CANCEL_OPTION
value is 2.
Here is the part of the JOptionPane class that shows the value:
/** Return value from class method if CANCEL is chosen. */
public static final int CANCEL_OPTION = 2;
As you already have a case 2:
and the CANCEL_OPTION
also returns 2, you have to change it. For example, if you change to case 6:
it will work. Give it a try.
here you can see all the values that a JOptionPane has, so you can modify your case according to other values, so you don't get this duplicate case error anymore.