2015-09-19 89 views
0

这里是什么,我试图做一个说明:日期/月验证

首先,程序将打印“进入本月>”,然后用户必须 输入一个数字。如果输入的号码不在1到12的范围内 由于以下错误消息被打印为“不正确的月份”,并且 程序结束。如果输入的月份正确,程序将打印“输入月份的日期”,然后用户必须输入 to-speech。如果该号码不对应于前一个月的 中的某一天,则会输出以下错误消息:“月份的日期为 ”。然后程序结束。如果输入的月份编号为 并且输入的月份的日期是正确的,则程序应该打印“正确的日期”,然后终止。

,但我得到下面这个错误,我想不通为什么

Exception in thread "main" java.lang.NumberFormatException: For input string: "java.util.Scanner[delimiters=\p{javaWhitespace}+][position=0][match valid=false][need input=false][source closed=false][skipped=false][group separator=\ ][decimal separator=\,][positive prefix=][negative prefix=\Q-\E][positive suffix=][negative suffix=][NaN string=\Q?\E][infinity string=\Q∞\E]" 
at java.lang.NumberFormatException.forInputString(NumberFormatException.java:48) 
at java.lang.Integer.parseInt(Integer.java:449) 
at java.lang.Integer.parseInt(Integer.java:499) 
at upp.main(upp.java:10) 


import java.util.Scanner; 
public class upp { 
public static void main(String[] args) { 

    int[] numberOfDaysEachMonth = new int[]{31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; 
    System.out.println("Enter month > "); 
    Scanner month = new Scanner(System.in); 
    int userInputMonth = Integer.parseInt(month.toString()); 
    if (userInputMonth > 0 && userInputMonth < 13) { 
     System.out.println("Enter the day of month > "); 
     Scanner day = new Scanner(System.in); 
     int userInputDay = Integer.parseInt(day.toString()); 
     if (userInputDay > 0 && userInputDay < numberOfDaysEachMonth[userInputMonth - 1]) { 
      System.out.println("Correct date."); 
     } else { 
      System.out.println("Wrong date."); 
     } 
    } else { 
     System.out.println("Wrong month."); 
    } 
} 
} 

回答

1
int userInputMonth = Integer.parseInt(month.toString()); 

应该是出于以下语句中:

// Read as string, convert to int. 
int userInputMonth = Integer.parseInt(month.next()); 

// Read as int using nextInt() 
int userInputMonth = month.nextInt(); 


Scanner day = new Scanner(System.in); 

创建Scanner的对象后,无需再次创建它。使用相同的对象来读取System.in的更多输入。它应该是以下任一种语句。

// Read as string, convert to int. 
int userInputDay = Integer.parseInt(month.next()); 

// Read as int using nextInt()  
int userInputDay = month.nextInt(); 

您还需要在当天检查,而不是<使用<=条件。

if (userInputDay > 0 && userInputDay <= numberOfDaysEachMonth[userInputMonth - 1]) 

PS:重命名Scanner对象像scannerread然后用它阅读既天。


import java.util.Scanner; 

public class upp { 
    public static void main(String[] args) { 

    int[] numberOfDaysEachMonth = new int[]{31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; 
    System.out.println("Enter month > "); 
    Scanner scanner = new Scanner(System.in); 
    int userInputMonth = Integer.parseInt(scanner.next()); 
    if (userInputMonth > 0 && userInputMonth < 13) { 
     System.out.println("Enter the day of month > "); 

     int userInputDay = Integer.parseInt(scanner.next()); 
     if (userInputDay > 0 && userInputDay <= numberOfDaysEachMonth[userInputMonth - 1]) { 
     System.out.println("Correct date."); 
     } else { 
     System.out.println("Wrong date."); 
     } 
    } else { 
     System.out.println("Wrong month."); 
    } 
    } 
} 
+0

我没有改变的Integer.parseInt(month.next());和Integer.parseInt(month.next()); .......但是当我运行该程序它说,无论我输入的日期/月份是否正确 –

+0

添加System.out.println()对于这两个和尝试。 – YoungHobbit

+0

你是否更新了两个地方。然后它工作正常。 'Scanner day = new Scanner(System.in);'这不是必需的,使用同样的'month'对象读取'day'值。 – YoungHobbit

1
int userInputMonth = Integer.parseInt(month.toString()); 

犯规任何意义,因为一个月是扫描仪的对象和你把它的字符串表示转换为int,它总是会给你这个异常,而你只需要做

int userInputMonth = month.nextInt(); 

打印蛾的值,以检查其是否为有效整数或没有。

day变量相同,您也可以重复使用month扫描仪。完整的代码

try { 
    int[] numberOfDaysEachMonth = new int[] {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; 
    System.out.println("Enter month > "); 
    Scanner month = new Scanner(System. in); 
    int userInputMonth = month.nextInt(); 
    if (userInputMonth > 0 && userInputMonth < 13) { 
     System.out.println("Enter the day of month > "); 
     int userInputDay = month.nextInt(); 
     if (userInputDay > 0 && userInputDay < numberOfDaysEachMonth[userInputMonth - 1]) { 
      System.out.println("Correct date."); 
     } else { 
      System.out.println("Wrong date."); 
     } 
    } else { 
     System.out.println("Wrong month."); 
    } 
} catch (Exception e) { 
    System.out.println(e); 
} 

DEMO