+ 2

How do i make my string switch case insesitive?

Hey, ok so this is what i have and it wont work, it works with .toUpperCase() but if i type "Hello" theres no output becuase of the capital H. i basicly want it to ignore case without having to use if statements if possible. thanks String x; Scanner y = new Scanner(System.in); x = y.nextLine(); switch(x.equalsIgnoreCase()){ case "hello": system.out.println("hello how are you?"); break;

15th Jun 2017, 7:23 PM
D_Stark
D_Stark - avatar
6 Antworten
15th Jun 2017, 7:37 PM
Limitless
Limitless - avatar
+ 8
x.toUpper() or x.toLower() Will work because it is still x. it is just making x be HELLO or hello.. then it checks that x against your case arguments. x.equalIgnoreCase() is trying to compare x to an argument that is missing. it isn't making x ignorecase and checking it with an ignored case characteristic. it wants x.equalsIgnoreCase("a string argument to check x againts"); it will work as an if statement. if(x.equalsIgnoreCase("hello"); but not as a switch case
15th Jun 2017, 7:53 PM
LordHill
LordHill - avatar
+ 3
this uses x.toUpperCase() and all input is checked against uppercase.EDIT I removed my code, it was the wrong version lol
15th Jun 2017, 8:16 PM
Russel Reeder
Russel Reeder - avatar
+ 3
nvm, ur talking java. Tho looking at @Limitless's code, seems same concept applies
15th Jun 2017, 8:18 PM
Russel Reeder
Russel Reeder - avatar
+ 2
@ joker that helped alot thank you
15th Jun 2017, 11:07 PM
D_Stark
D_Stark - avatar
+ 2
np
15th Jun 2017, 11:28 PM
LordHill
LordHill - avatar