Reputation:
I keep getting the error message the operator type is undefined for the args
when I use any boolean operators in java. Do I have to import a boolean class or something?
import java.util.Scanner; //imports Scanner class
public class LeapYear {
public static void main (String[] args) {
//create Scanner object
Scanner input = new Scanner(System.in);
//declare variables
int year;
//get input
System.out.println("Enter year: ");
year = input.nextInt();
//create if statement
if ((year/4) && !(year/100)){
System.out.println("Leap Year");
}
else{
System.out.println("Not a Leap Year");
}
}
}
Upvotes: 0
Views: 87
Reputation: 5376
try this instead of division "/" use mode "%".
if ((year % 4 == 0) && (year % 100 != 0)) {
Upvotes: 0
Reputation: 239
(year/4) && !(year/100)
Neither of these integer operations equate to booleans. You might want to try something like:
if(year%4 == 0)
or something along those lines. I know the leap-year logic isn't perfect there, but the point is you need to be making some sort of comparison (==).
Upvotes: 0
Reputation: 178283
Unlike C/C++, you cannot treat int
values as booleans
. You must explicitly compare them to zero to create the boolean
result. Additionally, for leap year calculations, you want to compare the remainder when dividing, so us %
instead of /
:
if ((year % 4 == 0) && (year % 100 != 0)) {
Don't forget about years divisible by 400, which are leap years. I'll leave that change to you.
Upvotes: 4