0

I am new to programming and I am writing code for this assignment in school in which I need to have the user enter a month (first three letters), and then a year (taking in to account of leap years). This is what I have done:

public static void main(String[] args) {

    Scanner input = new Scanner(System.in);
    System.out.print("Enter a year: ");
    int Year = input.nextInt();
    System.out.print("Enter a month (first three letters with the first"
            + " letter uppercase): ");
    String Month = input.next();

    String ThirtyOne = "Jan" + "Mar" + "May" + "Jul" + "Aug" + "Oct" + "Dec";
    String DaysThirtyOne = ThirtyOne.substring(21) + "31";

    String Thirty = "Apr" + "Jun" + "Sep" + "Nov";
    String DaysThirty = Thirty.substring(12) + "30";

    String TwentyEight = "Feb";
    String DaysTwentyEight = TwentyEight.substring(3) + "28";


    if (ThirtyOne.contains(Month)) {
        System.out.println(Month + " " + Year + " has " + DaysThirtyOne 
                + " days in it.");
    }
    if (Thirty.contains(Month)) {
        System.out.println(Month + " " + Year + " has " + DaysThirty 
                + " days in it.");
    }
    if (TwentyEight.contains(Month)) {
        System.out.println(Month + " " + Year + " has " + DaysTwentyEight 
                + " days in it.");
    }

My code probably doesn't look very professional and I'm sure many of you would have gone about what I've completed so far a different way but what I am having trouble with now is taking the Year input from the top and using that in an algorithm to determine if it is a leap year. I keep getting an error when I try to do it because the year is an int and NetBeans says that it can't convert int to boolean. I don't want to make another class to convert it though. What do I do to get that input from the user and use it to determine if it is a leap year? I already know that the year needs to be divisible by 4, 100, and 400.

2
  • Where are you using year variable in your code as a boolean? You are just taking user input in your int year variable. Commented Sep 30, 2014 at 21:40
  • @Moni I did just change it to boolean Year = nextBoolean(); but how do I use that variable now in a leap year algorithm? Commented Sep 30, 2014 at 21:42

1 Answer 1

1

You can add a method before main()

  public static boolean isLeapYear(int year) {
  if (year % 4 != 0) {
    return false;
  } else if (year % 400 == 0) {
    return true;
  } else if (year % 100 == 0) {
    return false;
  } else {
    return true;
  }
}

And then check if this year is a leap Year after input user :

int Year = input.nextInt();
System.out.println("Is this leap year ? " + isLeapYear(Year));
Sign up to request clarification or add additional context in comments.

3 Comments

is there a way I can do this within the main method. I just want to take the Year input and do a function for it being divisible by 4, 100, and 400.
You could write it on one line instead as: System.out.print("Is this leap year ?" + Year % 4 == 0 && (Year % 100 != 0 || Year % 400 == 0));
so ? do not forget to validate my answer

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.