每次输入String或Char时,我的read()方法都会崩溃.我怎么能让它只接受整数值.当我输入一个int,它工作正常,但当我输入一个字符串或字符串时,我得到一个重复的"输入帐户打开的那天:null"错误.我必须终止程序才能阻止它.
private void readDay(Scanner keyboardIn) {
boolean success = false;
while (!success) {
try {
System.out.print("Enter the day the account opened: ");
int d = keyboardIn.nextInt();
dateOpened.setDay(d);
success = true;
} catch (Exception e) {
System.out.println(e.getMessage());
}
}
}
// Enter the month, checking for error
private void readMonth(Scanner keyboardIn) {
boolean success = false;
while (!success) {
{
try {
System.out.print("Enter the month the account opened: ");
int m = keyboardIn.nextInt();
dateOpened.setMonth(m);
success = true;
} catch (Exception e) …Run Code Online (Sandbox Code Playgroud) java ×1