程序在尝试退出程序时抛出 NumberFormatException

Program throws NumberFormatException when trying to exit program

这周我不得不写这个 DateConverter 程序作为作业。该程序应该将用户输入作为 month/day 格式的字符串,然后将输入转换为字母数字(即 1/21 至 1 月 21 日)。程序 运行s 直到用户输入 "exit" 退出。我知道我必须使用 while 循环来执行此操作,但每次我尝试 运行 并退出时,它都会抛出:

Exception in thread "main" java.lang.NumberFormatException: For input string: "Exit"
at java.lang.NumberFormatException.forInputString(Unknown Source)
at java.lang.Integer.parseInt(Unknown Source)
at java.lang.Integer.parseInt(Unknown Source)
at DateConverter.Date(DateConverter.java:60)
at DateConverter.main(DateConverter.java:115)

我用来退出程序的代码是:

while (true) { 
        System.out.println("Enter a numeric date formatted as month/day or \"Exit \" to quit.");

        if (sc.equals("Exit") || sc.equals("exit")) {
            System.out.println("Goodbye");
            System.exit(0);

我已将 if 语句移动到几个地方以尝试清除异常,但它不会接受。我还用谷歌搜索并查看了其他几个与此类似的问题,并尝试根据这些建议修复程序,但没有任何效果。谁能指出我正确的方向?这是我的其余代码:

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Scanner;

public class DateConverter {    
static final ArrayList<Integer> THIRTY_DAYS = new ArrayList<>(Arrays.asList(4, 6, 9, 11));

static void Date() {
    Scanner sc = new Scanner(System.in);
    String month = null; 

    System.out.println("Welcome to the date converter!");

    while (true) { 
        System.out.println("Enter a numeric date formatted as month/day or \"Exit \" to quit.");

        if (sc.equals("Exit") || sc.equals("exit")) {
            System.out.println("Goodbye");
            System.exit(0);
        }
        String[] input = sc.nextLine().split("/");

        int[] dateInput = new int[input.length];
        for (int i = 0; i < input.length; i++) {
           dateInput[i] = Integer.parseInt(input[i]);
    }

    if (dateInput[0] == 1) {
        month = "January";
    } else if (dateInput[0] == 2) {
        month = "February";
    } else if (dateInput[0] == 3) {
        month = "March";
    } else if (dateInput[0] == 4) {
        month = "April";
    } else if (dateInput[0] == 5) {
        month = "May";
    } else if (dateInput[0] == 6) {
        month = "June";
    } else if (dateInput[0] == 7) {
        month = "July";
    } else if (dateInput[0] == 8) {
        month = "August";
    } else if (dateInput[0] == 9) {
        month = "September";
    } else if (dateInput[0] == 10) {
        month = "October";
    } else if (dateInput[0] == 11) {
        month = "November";
    } else if (dateInput[0] == 12) {
        month = "December";
    }

    try {
        if (dateInput[0] > 12 || dateInput[0] <= 0) {
            throw new MonthException();
        } else if (dateInput[0] <= 5 && dateInput[1] <= 5 ) {
            throw new InputException();
        } else if (THIRTY_DAYS.contains(dateInput[0]) && dateInput[1] > 30) {
            throw new DayException();
        } else if (dateInput[1] > 31 || dateInput[1] <= 0) {
            throw new DayException();
        } else if (dateInput[0] == 2 && dateInput[1] > 29) {
            throw new DayException();
        }
        System.out.println("The date is " + month + " " + dateInput[1]);

    } catch (MonthException ex) {
        System.out.println(ex.getMessage());
    } catch (DayException ex) {
        System.out.println(ex.getMessage());
    } catch (InputException ex) {
        System.out.println(ex.getMessage());
    }

}       
} 

    public static void main(String[] args) {
        DateConverter.Date();
    }
}

class MonthException extends Exception {
private String month; 

MonthException() {
    super("Month Exception: Months must be between 1 and 12 inclusively.");
    this.month = month;
}

public String getMonth() {
    return month;
    }
  } 

class InputException extends Exception {
private String input;

InputException() {
    super("Input Exception: The inputed date is in the wrong format.");
    this.input = input;
}
public String getInput() {
    return input;
}
}

class DayException extends Exception {
private String day;

DayException() {
    super("Day Exception: This day is in the wrong range for the month provided.");
    this.day = day;
}

public String getDay() {
    return day;
}
}

提前致谢!

编辑: 这是更新后的代码片段,其中包含它抛出的错误。如果我输入日期,程序现在将抛出异常,但退出正常。我的印象是我的所有代码都需要在循环中才能继续 运行ning,但也许我错了。谁能指出我正确的方向?抱歉,如果我不能立即理解。我仍处于 Java 编程的第一阶段。

static void Date() {
    Scanner sc = new Scanner(System.in);
    String month = null; 

    System.out.println("Welcome to the date converter!");

    while (true) { 
        System.out.println("Enter a numeric date formatted as month/day or \"Exit \" to quit.");

        // Quits program if user enters "exit"
        String inputStr = sc.nextLine();
        if (inputStr.equalsIgnoreCase("Exit")) {
            System.out.println("Goodbye");
            System.exit(0);
        }

        String[] input = sc.nextLine().split("/");

        int[] dateInput = new int[input.length];
        for (int i = 0; i < input.length; i++) {
           dateInput[i] = Integer.parseInt(input[i]);
    }

错误:

Welcome to the date converter!
Enter a numeric date formatted as month/day or "Exit " to quit.
1/21

Exception in thread "main" java.lang.NumberFormatException: For input string: ""
at java.lang.NumberFormatException.forInputString(Unknown Source)
at java.lang.Integer.parseInt(Unknown Source)
at java.lang.Integer.parseInt(Unknown Source)
at DateConverter.Date(DateConverter.java:47)
at DateConverter.main(DateConverter.java:104)

如果 input 不是 integer 且无法解析,则下面这部分代码将抛出 NumberFormatException。

       dateInput[i] = Integer.parseInt(input[i]);

sc 是 Scanner 对象而不是 String 我猜您想提示输入 String 所以 ....

    System.out.println("Enter a numeric date formatted as month/day or \"Exit\" to quit.");

    String inputStr = sc.nextLine();
    if (inputStr.equalsIgnoreCase("Exit")) {
        System.out.println("Goodbye");
        System.exit(0);
    }
    // more validation could be done here
    String[] input = inputStr.split("/");

您还可以捕获抛出的异常以重新提示用户输入 correct