I have tried a few different methods, like print(boolean isLeapYear) and a few others, but I can not figure out how to get it to work. It always says that I have a missing class (boolean is primitive, does it need one?) Anyways, If the isLeapYear if-else statements are wrong, I'm not worried about those.. I just need to figure out how to print out the value of the boolean; any help / point in the right direction is greatly appreciated =]
import java.util.Scanner;
public class booleanfun {
boolean isLeapYear;
public static void main(String[] args)
{
System.out.println("Enter a year to determine if it is a leap year or not: ");
Scanner kboard = new Scanner(System.in);
int year = kboard.nextInt();
}
public boolean isLeapYear(int year)
{
if (year % 4 != 0)
isLeapYear = false;
else if ((year % 4 == 0) && (year % 100 == 0))
isLeapYear = false;
else if ((year % 4 == 0) && (year % 100 == 0) && (year % 400 == 0))
isLeapYear = true;
else
isLeapYear = false;
System.out.println(isLeapYear);
System.out.println(boolean isLeapYear);
return isLeapYear;
}
}
There are a couple of ways to address your problem, however this is probably the most straightforward:
Your main
method is static, so it does not have access to instance members (isLeapYear
field and isLeapYear
method. One approach to rectify this is to make both the field and the method static as well:
static boolean isLeapYear;
/* (snip) */
public static boolean isLeapYear(int year)
{
/* (snip) */
}
Lastly, you're not actually calling your isLeapYear
method (which is why you're not seeing any results). Add this line after int year = kboard.nextInt();
:
isLeapYear(year);
That should be a start. There are some other best practices you could follow but for now just focus on getting your code to work; you can refactor later.
First of all, your variable "isLeapYear" is the same name as the method. That's just bad practice.
Second, you're not declaring "isLeapYear" as a variable.
Java is strongly typed so you need a
boolean isLeapYear;
in the beginning of your method.
This call:
System.out.println(boolean isLeapYear);
is just wrong. There are no declarations in method calls.
Once you have declared isLeapYear to be a boolean variable, you can call
System.out.println(isLeapYear);
UPDATE:
I just saw it's declared as a field. So just remove the line System.out.println(boolean isLeapYear);
You should understand that you can't call isLeapYear from the main() method. You cannot call a non static method from a static method with an instance.
If you want to call it, you need to add
booleanfun myBoolFun = new booleanfun();
System.out.println(myBoolFun.isLeapYear);
I really suggest you use Eclipse, it will let you know of such compilation errors on the fly and its much easier to learn that way.
you should just remove the 'boolean' in front of your boolean variable.
Do it like this:
boolean isLeapYear = true;
System.out.println(isLeapYear);
or
boolean isLeapYear = true;
System.out.println(isLeapYear?"yes":"no");
The other thing ist hat you seems not to call the method at all! The method and the variable are both not static, thus, you have to create an instance of your class first. Or you just make both static and than simply call your method directly from your maim method.
Thus there are a couple of mistakes in the code. May be you shoud start with a more simple example and than rework it until it does what you want.
Example:
import java.util.Scanner;
public class booleanfun {
static boolean isLeapYear;
public static void main(String[] args)
{
System.out.println("Enter a year to determine if it is a leap year or not: ");
Scanner kboard = new Scanner(System.in);
int year = kboard.nextInt();
isLeapYear(year);
}
public static boolean isLeapYear(int year) {
if (year % 4 != 0)
isLeapYear = false;
else if ((year % 4 == 0) && (year % 100 == 0))
isLeapYear = false;
else if ((year % 4 == 0) && (year % 100 == 0) && (year % 400 == 0))
isLeapYear = true;
else
isLeapYear = false;
System.out.println(isLeapYear);
return isLeapYear;
}
}
public boolean isLeapYear(int year)
{
if (year % 4 != 0){
isLeapYear = false;
System.out.println("false");
}
else if ((year % 4 == 0) && (year % 100 == 0)){
isLeapYear = false;
System.out.println("false");
}
else if ((year % 4 == 0) && (year % 100 == 0) && (year % 400 == 0)){
isLeapYear = true;
System.out.println("true");
}
else{
isLeapYear = false;
System.out.println("false");
}
return isLeapYear;
}
There are several problems.
One is of style; always capitalize class names. This is a universally observed Java convention. Failing to do so confuses other programmers.
Secondly, the line
System.out.println(boolean isLeapYear);
is a syntax error. Delete it.
Thirdly.
You never call the function from your main routine. That is why you never see any reply to the input.
Source: Stackoverflow.com