Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I print out the value of this boolean? (Java)

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;
    }
}
like image 760
cutrightjm Avatar asked Jan 24 '12 13:01

cutrightjm


People also ask

Can you print a Boolean value in Java?

The println(boolean) method of PrintStream Class in Java is used to print the specified boolean value on the stream and then break the line. This boolean value is taken as a parameter.

How do I print boolean values?

Use %t to format a boolean as true or false .

How do you return a Boolean value in Java?

Java Boolean equals() method The equals() method of Java Boolean class returns a Boolean value. It returns true if the argument is not null and is a Boolean object that represents the same Boolean value as this object, else it returns false.


5 Answers

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;
    }
}
like image 97
janosch Avatar answered Oct 23 '22 02:10

janosch


System.out.println(isLeapYear);

should work just fine.

Incidentally, in

else if ((year % 4 == 0) && (year % 100 == 0))
    isLeapYear = false;

else if ((year % 4 == 0) && (year % 100 == 0) && (year % 400 == 0))
    isLeapYear = true;

the year % 400 part will never be reached because if (year % 4 == 0) && (year % 100 == 0) && (year % 400 == 0) is true, then (year % 4 == 0) && (year % 100 == 0) must have succeeded.

Maybe swap those two conditions or refactor them:

else if ((year % 4 == 0) && (year % 100 == 0))
    isLeapYear = (year % 400 == 0);
like image 41
Mike Samuel Avatar answered Oct 23 '22 01:10

Mike Samuel


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.

like image 38
ncmathsadist Avatar answered Oct 23 '22 02:10

ncmathsadist


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.

like image 29
Brian Driscoll Avatar answered Oct 23 '22 01:10

Brian Driscoll


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.

like image 45
Enrico Avatar answered Oct 23 '22 02:10

Enrico