Rajat
Rajat

Reputation: 692

Cannot use multiple Scanner objects in Java

I have started learning Java recently. I was learning to take user input using Scanner class when I Started getting an error. Here is the code:

Scanner userInput1 = new Scanner(System.in);
        String name = userInput1.nextLine();
        System.out.println("Hi "+ name);
        userInput1.close();

        Scanner userInput2 = new Scanner(System.in);
        int age = userInput2.nextInt();
        System.out.println(age);

I get the following error when I enter "Deadboy" as input:

Hi Deadboy
Exception in thread "main" java.util.NoSuchElementException
    at java.util.Scanner.throwFor(Scanner.java:862)
    at java.util.Scanner.next(Scanner.java:1485)
    at java.util.Scanner.nextInt(Scanner.java:2117)
    at java.util.Scanner.nextInt(Scanner.java:2076)
    at com.first.Main.main(Main.java:17)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:497)
    at com.intellij.rt.execution.application.AppMain.main(AppMain.java:134)

Process finished with exit code 1

I am unable to enter the value for "age". If, however, I comment the line "userInput1.close()", the code works.

What is the problem?

I am sorry if this question has been answered before. I found a similar question but I was not sure if its answers were the ones I was looking for.

Upvotes: 2

Views: 13772

Answers (4)

Ski
Ski

Reputation: 111

Several things. First of all, you do not need to make two Scanner objects. You can use one scanner object to scan in everything, the way you're using it. Two, there's no point in closing the scanner the way you're doing it. Why are you closing the scanner? Fix those, and run the codes again.

Upvotes: 2

Ved Agarwal
Ved Agarwal

Reputation: 565

There is actually no need to create Scanner object each time you want to read something from console. You can use the same Scanner object

Scanner scanner = new Scanner(System.in);
        String name = scanner.nextLine();
        System.out.println("Hi "+name);

        int age = scanner.nextInt();
        System.out.println("Age :" + age);

Upvotes: 2

Makoto
Makoto

Reputation: 106400

There's a couple of problems here.

First, you do not want to close the System.in stream. Other parts of the program may be using it, and you don't want to interfere with their normal operation.

Second, there's no benefit to creating more than one Scanner object. It's simply reading input from a stream, and having more than one reference to that stream isn't necessary or beneficial to your operations.

To that end, the fix to this would be straightforward:

  • Use only one instance of the Scanner attached to System.in, and
  • Remove the close() method call.

Upvotes: 4

Vladislav Lezhnin
Vladislav Lezhnin

Reputation: 837

The problem here is that when you are closing Scanner userInput1, it closes input source, with which it was created - in this case it is InputStream you got from accessing System.in. So when you call

Scanner userInput2 = new Scanner(System.in);

at this point System.in InputStream is already closed, and you can not longer work with it.

Upvotes: 3

Related Questions