将扫描器对象分配给变量时,程序崩溃

时间:2018-10-12 21:18:56

标签: java variables if-statement switch-statement java.util.scanner

我正在尝试创建一个程序,该程序读取格式为month/day/year的日期输入。不幸的是,我目前停留在声明int year = dateScan.nextInt();上。当我运行程序时,它在那行崩溃。但是,如果删除整个语句并将System.out.println("year: " + year);更改为System.out.println("year: " + dateScan());,则程序不会崩溃。我担心的是,我需要可变的年份来打印整个日期,因此末尾的打印语句System.out.println("\nThe date is: " + monthName + " " + day + "," + year);将起作用。我需要知道为什么它会在那条线上崩溃。谢谢!

公共类DateFormat {

public static void main(String[] args) {
    Scanner keyboard = new Scanner (System.in); // define a Scanner object attached to the keyboard
    System.out.print("Enter a date in the form month/day/year: "); // prompt user for input
    String inputString = keyboard.nextLine(); // read entire input line as a String

    Scanner dateScan = new Scanner (inputString); // define a Scanner object attached to a String

    // set the Scanner's delimiter to "zero or more spaces followed by a slash (/) followed by zero or more spaces"
    dateScan.useDelimiter("\\s*/\\s*"); 

    int month = dateScan.nextInt();
    if ( ! dateScan.hasNextInt()) // is the first input value an int?
    {
        String badInput = dateScan.next();
        System.out.println("Error: expected an integer, encountered: " + badInput);
        System.out.print("Try again later...");
        System.exit(1);
    }
    System.out.println("month: " + month); // safe to read first input value

    int day = dateScan.nextInt();
    if ( ! dateScan.hasNextInt()) // is the next input value an int?
    {
        String badInput = dateScan.next();
        System.out.println("Error: expected an integer, encountered: " + badInput);
        System.out.print("Try again later...");
        System.exit(1);
    }
    System.out.println("day: " + day); // safe to read next input value

    int year = dateScan.nextInt();
    if ( ! dateScan.hasNextInt()) // is the next input value an int?
    {
        String badInput = dateScan.next();
        System.out.println("Error: expected an integer, encountered: " + badInput);
        System.out.print("Try again later...");
        System.exit(1);
    }
    System.out.println("year: " + year); // safe to read next input value

    // the user entered an integer
    String monthName = "Unknown month name";
    switch(month)
{
        case 1: monthName = "January"; break;
        case 2: monthName = "February"; break;
        case 3: monthName = "March"; break;
        case 4: monthName = "April"; break;
        case 5: monthName = "May"; break;
        case 6: monthName = "June"; break;
        case 7: monthName = "July"; break;
        case 8: monthName = "August"; break;
        case 9: monthName = "September"; break;
        case 10: monthName = "October"; break;
        case 11: monthName = "November"; break;
        case 12: monthName = "December"; break;                           
}
    if (month < 1 || month > 12)
        System.out.println("Error: " + month + " is an invalid month value");
    else
        System.out.println("\nThe date is: " + monthName + " " + day + "," + year);      
}   

}

1 个答案:

答案 0 :(得分:0)

更改:

    int year = dateScan.nextInt();
    if ( !dateScan.hasNextInt()) // is the next input value an int?

    int year = dateScan.nextInt();
    if ( dateScan.hasNextInt()) // is the next input value an int?

由于您正在检查以下内容而发生错误:dateScan.hasNextInt(),并且由于dateScan没有您正在做的下一个元素

String badInput = dateScan.next()

这会导致异常:

Exception in thread "main" java.util.NoSuchElementException