+ 2

Comparar valor vacío de scanner de tipo int

Compare a void value from scanner (type int) https://code.sololearn.com/crCRg2BNHMS2/#java

23rd Nov 2018, 2:33 AM
...
2 Réponses
+ 2
nextInt only returns integers. If there isn't an integer available, it raises an exception. You have two choices: deal with the exception or test to see if an integer is available. This is your test: if(!sc.hasNextInt()){ vacio(); } else { i = sc.nextInt(); System.out.print("Gracias"); //Thanks }
28th Nov 2018, 9:17 PM
John Wells
John Wells - avatar
+ 2
Note: allocating a second scanner as your code attempts won't work. All of the input goes to the first one.
28th Nov 2018, 9:20 PM
John Wells
John Wells - avatar