matic
matic

Reputation: 311

at java.util.Scanner.throwFor(Unknown Source) error

  private static int posNum() {
            Scanner scan = new Scanner(System.in);
            int input = 0;
            boolean error;

            if (scan.hasNextInt()) {
                input = scan.nextInt();
                error = input <= 0;
            } else {
    28          scan.next();
                error = true;
            }
            while (error) {
                System.out.print("Invalid input. Please reenter: ");
                if (scan.hasNextInt()) {
                    input = scan.nextInt();
                    error = input <= 0;
                } else {
                    scan.next();
                    error = true;
                }
            }
            scan.close();
            return input;

        }

So the second time I call this method its returning the following error.

Exception in thread "main" java.util.NoSuchElementException
    at java.util.Scanner.throwFor(Unknown Source)
    at java.util.Scanner.next(Unknown Source)
    at q2.CylinderStats.posNum(CylinderStats.java:28)
    at q2.CylinderStats.main(CylinderStats.java:62)

The first call is rad = posNum(); which runs fine and then secondis height = posNum(); which doesn't allow a value to be entered before it goes to the error.

Thanks

Upvotes: 2

Views: 49721

Answers (3)

Ivan
Ivan

Reputation: 1

private double getDoubleValue(String messageText) {
        System.out.print(messageText);

        while (true) {
            if (scanner.hasNext() && scanner.hasNextDouble()) {
                return scanner.nextDouble();
            }

            System.out.print(INVALID_VALUE_DESCRIPTION);
            scanner.next();
        }
    }

Upvotes: 0

ben_joseph
ben_joseph

Reputation: 1681

The fact that it runs once and gives you trouble means it is not able to read anything from the stream.

From Java DOCs:

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

Which means it closes the System.in

So leaving the scanner opened will prevent the error the second time.

Remove scan.close(); and your program should work fine!

Upvotes: 2

Amit Deshpande
Amit Deshpande

Reputation: 19185

while calling next you should check if scanner has one.

 if(scan.hasNext())
 scan.next();

According to java doc of Scanner#next

NoSuchElementException if no more tokens are available

You can change your method like below

private static int posNum(Scanner scan) {
    int input = 0;
    boolean error = false;
    if (scan.hasNext()) {
        if (scan.hasNextInt()) {
            input = scan.nextInt();
            error = input <= 0;
        } else {
            scan.next();
            error = true;
        }
    }
    while (error) {
        System.out.print("Invalid input. Please reenter: ");
        if (scan.hasNextInt()) {
            input = scan.nextInt();
            error = input <= 0;
        } else {
            if (scan.hasNext())
                scan.next();
            error = true;
        }
    }
    return input;
}

And then call it like below

    Scanner scan = new Scanner(System.in);
    int i = posNum(scan);
    System.out.println(i);
    int j = posNum(scan);
    System.out.println(j);

Upvotes: 4

Related Questions