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

30th May 2019, 6:42 AM
Rishabh Doshi
Rishabh Doshi - avatar
2 Respuestas
+ 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; }
30th May 2019, 6:59 AM
Nova
Nova - avatar
+ 3
Rishabh Doshi Scanner myObj = newScanner(System.in); Int myInt = myObj.nextInt(); should give you an int.
30th May 2019, 6:52 AM
Dima Makieiev
Dima Makieiev - avatar