0

I keep getting this error and I do not know why :

Exception in thread "main" java.util.NoSuchElementException
at java.util.Scanner.throwFor(Scanner.java:907)
at java.util.Scanner.next(Scanner.java:1530)
at java.util.Scanner.nextInt(Scanner.java:2160)
at java.util.Scanner.nextInt(Scanner.java:2119)
at Daily.takingData(Daily.java:33)
at Daily.main(Daily.java:20)

this is my code:

import java.io.*;
import java.util.Scanner;

public class Daily    
{

    private static int size;

    public static void main(String[] args) throws Exception {
        System.out.println("Please enter amount of rows");
        Scanner scan1 = new Scanner(System.in);
        size = scan1.nextInt();
        scan1.close();

        System.out.println();
        takingData(size);
    }

    public static void takingData(int rows) throws Exception {
        System.out.println("Enter 1 To View Number of Markets");
        System.out.println("Enter 2 To View Start and End Dates of Markets");
        System.out.println("Enter 3 To View Start and End Dates of Contracts");
        System.out.println("Enter 4 To View Averages of Markets");
        System.out.println("Enter 0 To Quit Program");
        int choice = 0;
        Scanner scan2 = new Scanner(System.in);
        choice = scan2.nextInt();
        System.out.println("Got here");
        scan2.close();
        if (choice == 0)
            System.exit(0);
    }
} 

My Out put is :

Enter 1 To View Number of Markets
Enter 2 To View Start and End Dates of Markets
Enter 3 To View Start and End Dates of Contracts
Enter 4 To View Averages of Markets
Enter 0 To Quit Program
(error here)
Menelaos
  • 23,508
  • 18
  • 90
  • 155
user2455869
  • 151
  • 1
  • 13

4 Answers4

1

The problem is that you're closing the first instance of Scanner here

scan1.close();

which is closing the associated InputStream (System.in) - this prevents the second Scanner instance of reading from the stream.

Don't close the scanner. Also you could create a single instance of Scanner for reading all values.

From a design point of view I would move from static methods to an OO approach with the single Scanner instance created in the constructor of Daily and all methods becoming instance methods. This will help with testability of the Object.

public class Daily {

    private final Scanner scanner;

    public Daily() {
        scanner = new Scanner(System.in);
    }

    public int getRows() {

        System.out.println("Please enter amount of rows");
        return scanner.nextInt();
    }

    public static void main(String[] args) throws Exception {

        Daily daily = new Daily();
        int rows = daily.getRows();

        int mainOption = daily.getMainOption(rows);
        switch (mainOption) {
        case 0: // TODO: refactor to use enums
            System.exit(0);
        }
    }

    public int getMainOption(int rows) {
        System.out.println("Enter 1 To View Number of Markets");
        System.out.println("Enter 2 To View Start and End Dates of Markets");
        System.out.println("Enter 3 To View Start and End Dates of Contracts");
        System.out.println("Enter 4 To View Averages of Markets");
        System.out.println("Enter 0 To Quit Program");

        return scanner.nextInt();
    }
}
Reimeus
  • 158,255
  • 15
  • 216
  • 276
1

You are getting an error because you close your scanner right after you scan:

size = scan1.nextInt();
scan1.close();

and then try to scan again in takingData

remove the scan1.close(); that is outside of your takingData.

When you close a Scanner, the InputStream that it is scanning from is also closed, in this case your System.in is being closed.

When a Scanner is closed, it will close its input source if the source implements the Closeable interface.

Taken from Scanner javadocs

Aboutblank
  • 697
  • 3
  • 14
  • 31
0

The answer to your question is here: https://stackoverflow.com/a/13042296/1688441 for question: java.util.NoSuchElementException - Scanner reading user input

I am quoting:

When you call, sc.close() in first method, it not only closes your scanner but closes your System.in input stream as well. This you can verify by printing its status at very top of the second method as :

System.out.println(System.in.available()); 

So now when you re-instantiate, Scanner in second method, it doesn't find any open System.in stream and hence the exception.

Community
  • 1
  • 1
Menelaos
  • 23,508
  • 18
  • 90
  • 155
-1

Get rid of the initial int choice, and try this:

int choice = scan2.nextInt();

Shouldn't really make a difference, but it could help.

Samuel Bauer
  • 83
  • 1
  • 10