本周我不得不为家庭作业编写这个DateConverter程序。该程序应该将用户输入作为月/日格式的字符串,然后将输入转换为字母数字(即 1/21 到 1 月 21 日)。程序将一直运行,直到用户输入"退出"退出。我知道我必须使用 while-loop 来执行此操作,但是每次我尝试运行并退出时,它都会抛出:
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;
}
}
提前谢谢你!
编辑:这是更新代码的片段,其中包含它抛出的错误。如果我输入日期,程序现在将抛出异常,但退出正常。我的印象是我的所有代码都需要在循环内才能继续运行,但也许我错了。谁能指出我正确的方向?对不起,如果我没有立即理解。我仍然处于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)
sc
是扫描器对象而不是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
如果input
不是integer
并且无法解析,则下面的代码部分将抛出NumberFormatException。
dateInput[i] = Integer.parseInt(input[i]);