+ 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;
6 Antworten
+ 4
Maybe something like this?
https://code.sololearn.com/cG3t2bOHihjg/?ref=app
+ 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
+ 3
this uses x.toUpperCase() and all input is checked against uppercase.EDIT I removed my code, it was the wrong version lol
+ 3
nvm, ur talking java. Tho looking at @Limitless's code, seems same concept applies
+ 2
@ joker that helped alot thank you
+ 2
np