0
How do I ask for values from the user in Switch condition?
I used Scanner but it has a different data type than int. So switch is not accepting it. I then tried equating it with an int variable but of course it's not possible.. Plz help
2 Answers
+ 14
âș Take input using the scanner class and store it in a variable and use that variable to check cases of switch statements.
âș You can refer this for better understanding :
Scanner in = new Scanner(System.in);
System.out.println("Enter Your Choice : ");
int ch = in.nextInt();
switch (ch) {
case 1:
.........................;
break;
case 2:
.........................;
break;
default:
.........................;
break;
}
+ 3
Rishabh Doshi
Scanner myObj =Â newScanner(System.in);
Int myInt = myObj.nextInt(); should give you an int.