+ 1

Can someone please explain the error in this simple program?

import java.util.Scanner; public class First { public static void main(String[] args) { int num; Scanner in = new Scanner(System.in); System.out.println("Enter the number"); num= in.nextInt(); System.out.print("You entered " +num); } } Output Enter the number Warning\Error Exception in thread "main" java.util.NoSuchElementException at java.util.Scanner.throwFor(Scanner.java) at java.util.Scanner.next(Scanner.java) at java.util.Scanner.nextInt(Scanner.)

5th Sep 2017, 7:43 PM
Nidhi
Nidhi - avatar
4 Answers
+ 10
//i don't think there is any error in the program run it again , might some connection problem //its working fine ... i checked â˜ș
5th Sep 2017, 8:03 PM
Changed
Changed - avatar
+ 10
about your code, i did similars yesterday and i had a dubt. how we can controll that input is of the right tipe and not for example a letter?
5th Sep 2017, 11:05 PM
Davide Mazzeo
Davide Mazzeo - avatar
+ 10
@david ... for that u can use try and catch block ... hope it helps â˜ș
6th Sep 2017, 4:06 AM
Changed
Changed - avatar
+ 9
sometimes the IDE run the last project and cause problems . Try again. Also for me your code is right.
5th Sep 2017, 10:51 PM
Davide Mazzeo
Davide Mazzeo - avatar