1

Can someone tell me why this code is throwing the NoSuchElementException? I've used scanner before for input and output but haven't ever had a problem with it.

import java.util.*;

public class TaskRunner
{
    public static void main(String[] args)
    {
        Scanner keyboard = new Scanner(System.in);

        System.out.print("Enter the year: ");
        int year = keyboard.nextInt();
        System.out.print("\nEnter the month: ");
        int month = (keyboard.nextInt() - 1);
        System.out.print("\nEnter the day: ");
        int day = keyboard.nextInt();

        System.out.print(day);
    }
}

The exception is being thrown at line 10 since everyone's asking. Also heres the stack trace.

Enter the year: 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 TaskRunner.main(TaskRunner.java:10)
Th3Ac3
  • 29
  • 3

0 Answers0