Search code examples
javaif-statementbluej

Else statement not printing line?


I wrote a simple program that is supposed to display whether if a user inputted int is a leap year or not, and if so what leap year is it.

During running of the program whenever a number that was not supposed to be a leap year was inputted it did not print the else statement.

Note: This was written in the IDE BlueJ so io was automatically imported hence why I did not import it

/**
 * Reads a user inputted integer value and determines if it is a leap year
 * Created by Oh boy I suck at this
 * 9 September 2019
 */

import java.util.Scanner;

public class LeapYear
{
    public static int getYear(String prompt)
    {
        String newLine = System.getProperty("line.separator");
        int value = 0;
        boolean flag = true;
        while (flag)
        {
            Scanner scan = new Scanner(System.in);
            System.out.println(prompt + ": ");
            try
            {
                value = scan.nextInt();
                flag = false;
            }
            catch(java.util.InputMismatchException e)
            {
                System.out.println("What you have inputed was not an int.");
                System.out.println(newLine);
            }
        }
        return value;
    }

    public static void main (String[] args)
    {
        int year = getYear("Input the year ");
        final int leapYear = year/4;
        if(year % 4 == 0){
            if(year % 100 >= 1){
                if(year % 400 >= 1){
                    System.out.println("The year inputted: " + year + " is equivilant to " + leapYear + " leap year(s).");
                }
                else{
                    System.out.println("The year inputted: " + year + " is not a leap year.");
                }
            }
        }
    }
}

Solution

  • Let us look at your if statements, and remember the rules for leap years.

    Understanding leap years

    In general, a leap year is a year that is divisible by four, i.e. where the statement year % 4 == 0 is true.

    But: If a year is not a leap year, when it is divisible by 100. I.e. year % 100 == 0 is true.

    Exception: When that is also divisible by 400, i.e. year % 400 == 0 is true - then we have a leap year.

    Basically: divisible by four yields a candidate - you then have to study it further to make a final decision.

    Decision tree

    1. Is it a leap year candidate? year % 4 == 0 Otherwise: Display "not a leap year"
    2. Is it a leap year exception (every 100 years)? year % 100 == 0 Otherwise: Display "not a leap year"
    3. Is it a leap year exception exception (every 400 years)? year % 400 == 0 True => Display "leap year", False => Display "not a leap year"

    Your implementation

    Let's first look at your if statement.

    1. Your first if statement checks whether the year is divisible by four. This is so that you know whether you deal with a leap year candidate. So this is correct - but you forgot to deal with the case, when the year is NOT a leap year (so here you miss a possible "is not a leap year" output)

    2. Now it gets a bit confusing. You check whether the year is NOT divisible by 100. If a leap year candidate is NOT divisible by 100 it IS a leap year. So you can output success, but you have to deal with the "else" case.

    3. The third if is nested in the wrong if block. It belongs into the else block of the parent.

    Hints

    Try to get an understanding on how the input and the output relate to each other and try to hit every if / else.

    Whenever you write an if think about whether you need the corresponding else block.

    If things get weird, try to "trace" your program with "breadcrumb outputs": System.out.println("1"); System.out.println("2"); ... on every line where you deal with branching or looping (if,else,switch,while...) this will trace every step the program does on the command line. You can use this until your school encourages you to use a proper IDE.

    Don't forget: Practice makes perfect ;)

    Pseude code (spoiler)

    Only use this if you're completely stuck.

    if (year % 4 == 0) { // this might be a leap year - we need to dig further if (year % 100 == 0) { if (year % 400 == 0) { print "leap year" } else { print "not a leap year" } } else { print "leap year" } } else { print "not a leap year" }

    Bottom line

    Some of your logic is incorrect and you forget to implement the else branches.

    HTH