+ 1

why default case is not working. i would appreciate any help.

switch(y){ case "India": write(); break; case "France": write_q(); break; case default: System.out.print("no idea"); break; } }public static void write(){ System.out.print("Asia") ; } public static void write_q(){ System.out.print("Europe") ; } }

19th Feb 2017, 6:37 AM
shobhit
shobhit - avatar
6 odpowiedzi
+ 4
you have "case default:", replace that with "default:". You do not need the case in front of default.
19th Feb 2017, 4:55 PM
David Koplik
David Koplik - avatar
+ 2
i m using code playground so is it due to the software or codes
19th Feb 2017, 9:18 AM
shobhit
shobhit - avatar
+ 1
Code Playground is currently using Java version 1.8.0_65 or Java 8 update 65. So the use of a string in a switch shouldn't be an issue here. So there must be something else going on with your code that is not shown in the sample you provided.
19th Feb 2017, 9:34 AM
ChaoticDawg
ChaoticDawg - avatar
+ 1
import java.util.Scanner; public class Program { public static void main(String[] args) { Scanner w = new Scanner(System.in); String y = w.next(); remaining codes is this
19th Feb 2017, 12:45 PM
shobhit
shobhit - avatar
0
Which version of the JDK are you using? If it's Java 6 or older you can't switch on a string. Java 7 or newer should work fine. Given that in the code you presented we don't know the value of y nor can we see the actual implementation of your code it's very difficult to give an appropriate answer other than this. From what is here it appears that there is an extra closing curly brace after the switch statement.
19th Feb 2017, 7:48 AM
ChaoticDawg
ChaoticDawg - avatar
0
@David good catch don't know how i missed that. lol
19th Feb 2017, 10:18 PM
ChaoticDawg
ChaoticDawg - avatar