0

I am taking input using java Scanner by writing following code:

import java.util.Scanner;
class main{
    public static void main(String args[]){
        Scanner scan=new Scanner(System.in);
        System.out.println("Enter number");
        int i=scan.nextInt();
    }
}

but above code in throwing following exception:

Enter number
Exception in thread "main" java.util.NoSuchElementException
at java.base/java.util.Scanner.throwFor(Scanner.java:937)
at java.base/java.util.Scanner.next(Scanner.java:1594)
at java.base/java.util.Scanner.nextInt(Scanner.java:2258)
at java.base/java.util.Scanner.nextInt(Scanner.java:2212)
at main.main(main.java:7)
Tool completed with exit code 1
iram
  • 11
  • 2
  • 1
    What did you enter? – Henry Feb 25 '19 at 05:35
  • There isn't anything wrong with that chunk of code...can you post your full code and any inputs you tested? – Rohan Feb 25 '19 at 05:35
  • Possible duplicate of [How can I read input from the console using the Scanner class in Java?](https://stackoverflow.com/questions/11871520/how-can-i-read-input-from-the-console-using-the-scanner-class-in-java) – A.Najafi Feb 25 '19 at 05:38
  • 1
    I googled for that Exception specifically, and saw this post that talks about having closed a prior Scanner: https://stackoverflow.com/questions/13042008/java-util-nosuchelementexception-scanner-reading-user-input - Does this apply in your case? Have you, in the same run of your program, created another Scanner object and the closed it? This post suggests that that's a way to arrive at the problem you are seeing. If something in earlier code hasn't put the environment in a funny state, there's no reason the code you show us should be throwing that exception. – CryptoFool Feb 25 '19 at 05:39
  • How are you running this program? At the command line (which OS)? In an IDE (which one)? In some other type of environment? I've been Googling, and everything I see that gives any insight suggests that System.in is in a state where it can't provide any more input, and that is what leads to this error. If this is the only code you're running (this is happening at line 7 of your program, so maybe you are), then it must be something about the environment in which you're running the code. – CryptoFool Feb 25 '19 at 05:54
  • [https://stackoverflow.com/questions/34508824/input-using-scanner-class-in-java] https://stackoverflow.com/questions/34508824/input-using-scanner-class-in-java See above link. – karan shah Feb 25 '19 at 06:09
  • the scanner not stop for taking input. it just show following information: – iram Feb 26 '19 at 06:05
  • Enter number Exception in thread "main" java.util.NoSuchElementException at java.base/java.util.Scanner.throwFor(Scanner.java:937) at java.base/java.util.Scanner.next(Scanner.java:1594) at java.base/java.util.Scanner.nextInt(Scanner.java:2258) at java.base/java.util.Scanner.nextInt(Scanner.java:2212) at main.main(main.java:7) Tool completed with exit code 1 – iram Feb 26 '19 at 06:05
  • I am running this code inTexpad onwindows 7. – iram Feb 26 '19 at 06:10
  • Do you really mean you're running it Textpad, or are you just editing it there? If you're really running it from there, have you tried running this at a normal cmd prompt? Have you ever had a Scanner work without throwing this error? This seems like something about your environment to me, not your code. – CryptoFool Feb 26 '19 at 06:32

3 Answers3

1
import java.util.Scanner; 
class Input { 

public static void main(String[] args) {        

Scanner input = new Scanner(System.in);                System.out.print("Enter an integer: ");  
int number = input.nextInt();   
System.out.println("You entered " + number);
 }
}

Try this code.....

Here, input object of Scanner class is created. Then, the nextInt() method of the Scanner class is used to get integer input from the user.

To get long, float, double and Stringinput from the user, you can use nextLong(), nextFloat(), nextDouble() and next() methods respectively.

0

You are almost there. One note is you expect int value and if user enter e.g. string, then you get an exception.

public static int askIntNumber(Scanner scan) {
    while(true) {
        try {
            System.out.print("Enter number: ");
            return scan.nextInt();
        } catch (Exception e) {
            System.err.println("This is not an integer number");
        }
}
oleg.cherednik
  • 12,764
  • 2
  • 17
  • 25
0

You should try this:

import java.util.Scanner;
public class main {
    public static void main(String[]args) {
        Scanner sc=new Scanner(System.in);
        System.out.println("Enter number: ");
        int i=sc.nextInt();
    }
}

The block of code sc.nextInt scans the entire input and then puts it into play. This should solve your problem.

boi yeet
  • 82
  • 10