0

Switch case is taking value but only the default case is running.

int z = Integer.parseInt(x);
JOptionPane.showMessageDialog(null,+z);
    switch (z) {
    case ('1'):
        C.SetEmpInformation();
        break;
    case ('2'):
        C.UpdateEmpInformation();
        break;
    default:
        JOptionPane.showMessageDialog(null, "Invalid Identity");
}
3
  • '1' is a character, not the number 1. Is the value in the variable z 1 or 49 (the ASCII value of character 1)? Also as a stylistic thing, the parentheses in the case labels look weird and may confuse people. Commented Oct 20, 2018 at 16:14
  • try case 1: and case 2: Commented Oct 20, 2018 at 16:15
  • Note that your example would not have worked on Eclipse either. If it did, the chances are that the code was different. (This is basic Java ... not an IDE difference.) Commented Apr 28, 2020 at 3:03

3 Answers 3

3

Remove the single quotes in your case statements. They will be matched as char and not as int. '1' is a character and very different from the number 1.

 switch (z) {
   case 1: /* .... */
   case 2: /* .... */
   default : /* .... */
}

You wouldn't need the brackets as well.

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

Comments

1

The type you are checking in your case statements is wrong, you compare an int (z) to a char ('1').

You need to write your cases like this:

JOptionPane.showMessageDialog(null, + z); // btw, what is this + doing here?
    switch (z) {
    case 1:
        C.SetEmpInformation();
        break;
    case 2:
        C.UpdateEmpInformation();
        break;
    default:
        JOptionPane.showMessageDialog(null, "Invalid Identity");
        break;
}

2 Comments

thank you so much, it worked but this program is running happily in Eclipse.Why?
You haven't writte wrong code from the compiler's perspective, it seems valid to it. The problem was that it wasn't working the way you wanted it to...
0

It works for me in NetBeans too, so if it does not even start on your machine, you may have some verification tool, linter, something like that.
Anyway, what you write is correct Java code, just probably it will not do what you expect, and that is what the message may indicate.
You can switch on numbers, characters and Strings, all of them work. Just do not mix them: they either will not compile (mixing numbers/characters with String), or will work in a probably unexpected way (mixing numbers with characters), because 1 is a number but '1' is a character, which is represented by its ASCII code as a number, 49.

Test code:

String x="1";
int z=Integer.parseInt(x);
switch(z){
  case ('1'): System.out.println(z+" is '1'");break;
  default: System.out.println(z+" is not '1'");
}

x="49";
z=Integer.parseInt(x);
switch(z){
  case ('1'): System.out.println(z+" is '1'");break;
  default: System.out.println(z+" is not '1'");
}

x="1";
z=Integer.parseInt(x);
switch(z){
  case 1: System.out.println(z+" is 1");break;
  default: System.out.println(z+" is not 1");
}

switch(x.charAt(0)){
  case '1': System.out.println("\"1\".charAt(0) is '1'");break;
  default: System.out.println("\"1\".charAt(0) is not '1'");
}

switch(x){
  case "1": System.out.println("\""+x+"\" is \"1\"");break;
  default: System.out.println("\""+x+"\" is not \"1\"");
}    

Output:

1 is not '1'
49 is '1'
1 is 1
"1".charAt(0) is '1'
"1" is "1"

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.