Hi this is my first post, but I've lurked for a while usually finding answers to my questions in other threads, I couldn't find this one so I broke my posting cherry to ask.

I know I'm missing something, probably really simple, but possibly I'm doing this completely wrong.

I'm trying to write a method that determines if a year is a leap year and then returns it to main to print out whether it's a leap year or not, after I get this working I'm going to modify it so that it runs in a loop until the user exits, but for now I'm having an issue getting it to just work correctly.

I've tried it a few different ways, they all compile, but they all return the same thing, which is the output for the "true" value of the boolean "That is a leap year!" no matter what year I put in.

first try

import java.text.*; //formatted output
import java.util.Scanner; //keyboard input
public class leapyear
{
	public static boolean isLeapYear()
	{
		
		double year;
		Scanner keyboard = new Scanner(System.in);
		System.out.print("Please enter a year >");
			year = keyboard.nextDouble();	
		if ((year % 4 == 0) && (year % 100 != 0))
					return true;
			if
				(year % 400 == 0)
					return true;
				else 
					return false;
	}
	
	public static void main(String[] args)
	{
		
		
		boolean isLeapYear = isLeapYear();
		
		if (isLeapYear = true)
			System.out.print("That is a leap year!");
		if (isLeapYear = false)
				System.out.print("That is not a leap year.");
	}
		
}

second try

import java.text.*; //formatted output
import java.util.Scanner; //keyboard input
public class leapyear
{
	public static boolean isLeapYear()
	{
		
		double year;
		Scanner keyboard = new Scanner(System.in);
		System.out.print("Please enter a year >");
			year = keyboard.nextDouble();	
		if ((year % 4 == 0) && (year % 100 != 0) || (year % 400 == 0))
					return true;
			
				
				
				else 
					return false;
	}
	
	public static void main(String[] args)
	{
		
		
		boolean isLeapYear = isLeapYear();
		
		if (isLeapYear = true)
			System.out.print("That is a leap year!");
		    else
				System.out.print("That is not a leap year.");
	}
		
}

I think it's a problem with the boolean method, but I'm really inexperienced and I can't quite figure it out.

Thanks for taking a look :)

Recommended Answers

All 5 Replies

If you are not sure what the results of your conditions, print them all out and see what they return.
For example:
System.out.println("(year % 4 == 0)=" + (year % 4 == 0));

Do that for all the conditions and you should see where the problem is.

if (isLeapYear = true)

This get LOTS and LOTS of students in trouble.
A boolean variable does NOT need to be tested if it is equal to true. It is TRUE or FALSE.

if (isLeapYear)

The problem comes when the OP uses the wrong operator.

oh man thank you, I knew it was something simple, changing that "=true" part and looking at the conditions fixed it, you're awesome, thank you SO MUCH! :)

the fixed code looks like this

if (isLeapYear)
    System.out.print("That is a leap year!");
    else
        System.out.print("That is not a leap year.");

huge huge thanks!

@NormR1

if (isLeapYear = true)

the problem in this syntax is that in comparing one must use "==" double equal sign to compare if it is equal or not, if using only "=" one equal sign, it is only used for assigning data into a variable. the syntax in correct by using "true" as the parameter only the operator is incorrect, but still, the one you recommend is more easy but it can only be used in booleans.

@NormR1

if (isLeapYear = true)

the problem in this syntax is that in comparing one must use "==" double equal sign to compare if it is equal or not, if using only "=" one equal sign, it is only used for assigning data into a variable. the syntax in correct by using "true" as the parameter only the operator is incorrect, but still, the one you recommend is more easy but it can only be used in booleans.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.