Java 验证日期/月份

Java 验证日期/月份,java,date,Java,Date,以下是我尝试做的一个描述: 首先,程序将打印“输入月份>,”,然后用户必须 输入一个数字。如果输入的数字不在1到12之间 由于以下错误消息打印为“不正确的月份”,因此 节目结束。如果输入的月份正确,程序将 打印“输入月份>,”然后用户必须输入 演讲。如果数字与前一天的日期不一致 输入月份,将打印以下错误消息“错误的日期” 然后节目结束。如果两者都输入了月份号 如果输入的月份日期正确,则程序应 打印“正确的日期”,然后终止 但是我在下面得到了这个错误,我不知道为什么 Exception in th

以下是我尝试做的一个描述:

首先,程序将打印“输入月份>,”,然后用户必须 输入一个数字。如果输入的数字不在1到12之间 由于以下错误消息打印为“不正确的月份”,因此 节目结束。如果输入的月份正确,程序将 打印“输入月份>,”然后用户必须输入 演讲。如果数字与前一天的日期不一致 输入月份,将打印以下错误消息“错误的日期” 然后节目结束。如果两者都输入了月份号 如果输入的月份日期正确,则程序应 打印“正确的日期”,然后终止

但是我在下面得到了这个错误,我不知道为什么

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;
公共类upp{
公共静态void main(字符串[]args){
int[]numberofdaysachmonth=新的int[]{31,28,31,30,31,31,30,31,30,31};
System.out.println(“输入月份>”;
扫描仪月份=新扫描仪(System.in);
int userInputMonth=Integer.parseInt(month.toString());
如果(userInputMonth>0&&userInputMonth<13){
System.out.println(“输入月份>”;
扫描仪日=新扫描仪(系统英寸);
int userInputDay=Integer.parseInt(day.toString());
如果(userInputDay>0&&userInputDay
没有任何意义,因为month是一个Scanner对象,您将它的字符串表示形式转换为int,这将始终给您带来异常,而您只需这样做

int userInputMonth =  month.nextInt();
打印moth的值以检查其是否为有效整数

您对
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);
}
试试看{
int[]numberofdaysachmonth=新的int[]{31,28,31,30,31,31,30,31,30,31};
System.out.println(“输入月份>”;
扫描仪月份=新扫描仪(System.in);
int userInputMonth=month.nextInt();
如果(userInputMonth>0&&userInputMonth<13){
System.out.println(“输入月份>”;
int userInputDay=month.nextInt();
如果(userInputDay>0&&userInputDay

它应该是以下声明之一:

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

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


一旦创建了
扫描仪的对象
,就不需要再次创建它。使用同一对象读取
系统中的进一步输入。它应该是以下任一陈述

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

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


您还需要使用
我确实更改为Integer.parseInt(month.next());和Integer.parseInt(month.next());。。。。。。。但是,当我运行程序时,它会告诉我,无论我键入什么,日期/月份都是正确的。添加System.out.println()并重试。您是否更新了这两个位置。那么它工作得很好<代码>扫描仪日=新扫描仪(System.in)这不是必需的,使用相同的
对象读取
值。可能代码是错误的,因为如果我输入了错误的日期/月,它应该说错误的日期/月,但现在它只说无论我输入什么都是正确的谢谢阿披舍克,你让我度过了周末
 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);
// 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])
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.");
    }
  }
}