0

I'm completely new to Java, and I have no idea why it's giving me an error when ran. Can anyone explain to me what's the problem? Tried my best randomly trying different things, but got no where, and I'll probably learn more from someone who knows what they're doing. Thank you!

My codes:

import java.util.Scanner;

// Get date input and display results
// Parse using a delimiter

public class InputOutput3
{
    public static void main(String[] args)
    {
        // Declare variables
        String dateIn, input;
        int month, day, year;
        Scanner scan, scann;

        // Initialize variables
        scan = new Scanner(System.in);

        // Prompt and wait for input
        System.out.print("Enter enter the date (mm/dd/yy) > ");
        dateIn = scan.nextLine();
        scan.close();

        // Analyze value entered
        scan = new Scanner(dateIn);
        scan.useDelimiter("/");
        month = scan.nextInt();
        day = scan.nextInt();
        year = scan.nextInt();

        // Display results
        System.out.println("The month is " + month);
        System.out.println("The day is " + day);
        System.out.println("The year is " + year);

        //Get their name
        scann = new Scanner(System.in);
        System.out.print("Please enter your name: ");
        input = scann.nextLine();
        System.out.print("Your name is " +input);


        // Close resources
        scan.close();
        scann.close();
    }
}

Results:

Please enter your name: 

Exception in thread "main" java.util.NoSuchElementException: No line found
    at java.util.Scanner.nextLine(Unknown Source)
    at InputOutput3.main(InputOutput3.java:38)
Tinyfool
  • 1,412
  • 2
  • 16
  • 40
Webyr
  • 3
  • 2
  • 1
    *Tried my best randomly trying different things* slap whomever taught you this was how to program. –  Jan 22 '15 at 05:00

2 Answers2

0

At line 21 you close the Scanner that is reading from System.in, which closes all resources related to it (meaning System.in). Don't close the Scanner and you should be good.

Kick Buttowski
  • 6,371
  • 12
  • 34
  • 54
kirbyquerby
  • 725
  • 5
  • 16
-1

You really don't need to use two scanners. There are better classes for parsing dates. But if you're going to use two scanners, one of them to parse the date, then you should leave the scanner connected to System.in open to read from the console, and the scanner reading dateIn should be attached to scann.

ramsey0
  • 1,357
  • 1
  • 11
  • 10