方法错误,类不能应用于不同的类型



我想将一个字符串从主方法转换为另一个方法中的整数,但我得到一个错误。

`    public static void main(String[] args) 
 {
      System.out.println("Enter a date (use the format -> (MM/DD/YYYY)");
      //declare Scanner
      Scanner in = new Scanner (System.in);
      System.out.println("Enter a month (MM): ");
      String month = in.nextLine();
      System.out.println("Enter a day (DD): ");
      String day = in.nextLine();
      System.out.println("Enter a year (YYYY): ");
      String year = in.nextLine();
    String enteredDate = month + "/" + day + "/" + year;
    if (main.isValidDate(enteredDate))
      {
        main.leapYearCheck();
    }
}
private boolean isValidDate(String enteredDate) 
 {
    //logic
    parsedDate = null;// if it's valid set the parsed Calendar object up.
    return true;
}
// other code
private void leapYearCheck(String year) 
 {
        //leapyear
        int theYear = Integer.parseInt(year);
        if (theYear < 100) 
        {
            if (theYear > 40) 
            {
                theYear = theYear + 1900;
            }
            else 
            {               
                theYear = theYear + 2000;
            }
        }
        if (theYear % 4 == 0) 
        {
            if (theYear % 100 != 0) 
            {
                System.out.println(theYear + " is a leap year.");
            }
            else if (theYear % 400 == 0) 
            {
                System.out.println(theYear + " is a leap year.");
            }
            else 
            {
                System.out.println(theYear + " is not a leap year.");
            }
        }
        else 
        {
            System.out.println(theYear + " is not a leap year.");
        }
}//end of leap year
//other code }`

我得到错误:Date.java:31: error: method leapYearCheck in class Date cannot be applied to given types; main.leapYearCheck(); ^ required: String found: no arguments reason: actual and formal argument lists differ in length 1 error

我不明白这个错误。说我需要一个字符串,因为这个方法使用一个整数(我想)我需要返回一个字符串??我该如何解决这个问题?

您需要按照定义将年份以字符串形式传递给leapYearCheck方法

pass year in main method as:

if (main.isValidDate(enteredDate)) {
    main.leapYearCheck(year);
}

"

最新更新