4

I've tried a couple of things with the while loop and can't seem to get it to work. I want to keep requesting user input until the user inputs the number 0, here is the code I have so far:

import java.util.*;

public class Task10 {

    public static void main(String[] args) {
        System.out.println("Enter a year to check if it is a leap year");
        Scanner input = new Scanner(System.in);
        int year = input.nextInt();

        if ((year % 4 == 0) || ((year % 400 == 0) && (year % 100 != 0)))
            System.out.println(year + " is a leap year");
        else
            System.out.println(year + " is not a leap year");
    }
}
2
  • Your leap year logic is wrong. Commented Oct 9, 2015 at 10:44
  • Although not really what you asked, I see that you posted after the release of JDK 8 which included the introduction of the date-time API and therefore you can use [static] method isLeap (of class java.time.Year) rather than do the calculation yourself. Commented Oct 3, 2023 at 8:34

4 Answers 4

9

Use a while loop above input line as:

 while(true)

And, use if condition to break.

if(year == 0)
    break;

Also, condition for leap year is wrong in your code. It should be:

if((year % 100 == 0 && year % 400 == 0) || (year % 4 == 0 && year % 100 != 0))
    //its a leap year
else
    //its not

PS: As in comments, I'll give a complete code:

import java.util.*;

public class Task10 {

public static void main(String[] args) {
    System.out.println("Enter a year to check if it is a leap year");
    while(true){
    Scanner input = new Scanner(System.in);
        int year = input.nextInt();
        if(year == 0)
            break;
        if((year % 100 == 0 && year % 400 == 0) || (year % 4 == 0 && year % 100 != 0))
            System.out.println(year + " is a leap year");
        else
            System.out.println(year + " is not a leap year");
    }
}

}
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks mate, worked a charm. Still getting used to while loops and this explanation sunk in!
3

You need to do something to keep you input loop running until a stopping condition is encountered (which in your case is that when the user inputs 0)

// First get the scanner object with the input stream
Scanner sc = new Scanner(System.in); 

// Just using do-while here for no reason, you can use a simple while(true) as well
do{
    int input = sc.nextInt();  // read the next input
    if (int == 0) { // check if we need to exit out
        // break only if 0 is entered, this means we don't want to run the loop anymore
        break;
    } else {
        // otherwise, do something with the input
    }
} while(true); // and keep repeating

Comments

0

You should place your input taking code inside a while loop aned execute while loop untill year is 0 or lesser.

public static void main(String[] args) {
        int year = 1;
        while(year > 0)
        {
            System.out.println("Enter a year to check if it is a leap year");
            Scanner input = new Scanner(System.in);
            year = input.nextInt();
            if ((year % 4 == 0) || ((year % 400 == 0) && (year % 100 != 0)))
                System.out.println(year + " is a leap year");
            else
                System.out.println(year + " is not a leap year");
        }


    }

4 Comments

Why would you make scope of year variable local of main, when it is never required outside the while loop ?!
@vish4071 it reduces the number of lines of code needs to be written instead of marking the while as true at first and then checking for condition if year is 0 to break the loop I felt I can make the beginner understand better this way
It is bad practice. You should try and declare / define variables to use only when they are required. Make it rule of the thumb. A few LoC has no effect on project, but, scope of variables and memory etc makes a lot of difference.
But since it is a small program, it does not matter much :)
-1

Simple and best

Scanner sc=new Scanner(System.in);

while(sc.hasNextInt()){ int year = input.nextInt();

    if ((year % 4 == 0) || ((year % 400 == 0) && (year % 100 !=0)))
        System.out.println(year + " is a leap year");
    else
        System.out.println(year + " is not a leap year");

}


Until you enter a valid input , the loop will contionue

3 Comments

The single line of code, in your answer, is not formatted and does not compile. I assume that sc is an instance of java.util.Scanner. If that instance was created like so: new Scanner(System.in), then sc.hasNext() will never return false, so how does that answer the original question?
Update for more redable, instance sc created only once, when you enter invalid input loop will be break
Still haven't learned how to format code using markdown? By the way, the code in your answer still doesn't compile.

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.