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

115,805

Solution 1

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);

Solution 2

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.

Solution 3

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;
    }
}

Solution 4

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.

Solution 5

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.

Share:
115,805
cutrightjm
Author by

cutrightjm

Updated on July 26, 2020

Comments

  • cutrightjm
    cutrightjm almost 4 years

    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;
        }
    }
    
  • ncmathsadist
    ncmathsadist over 12 years
    There are other problems as well.
  • Adel Boutros
    Adel Boutros over 12 years
    For the down voters, can you please tell me why this is useless?
  • Adel Boutros
    Adel Boutros over 12 years
    @ekaj did you see the last line dude? it says return isLeapYear
  • Ben van Gompel
    Ben van Gompel over 12 years
    And it's even printing a different value (from isLeapYear) in the 3rd if. Why not just System.out.println(isLeapYear); before returning?
  • Adel Boutros
    Adel Boutros over 12 years
    @BenvanGompel Oh my god, did you hear about Typo mistake?
  • StartingGroovy
    StartingGroovy over 12 years
    @ekaj He told you exactly how to make it work. He also gave you tips that will help you in the long run. To add onto what he is saying, you should change either the name of your boolean or the method as they have the same name. These are helpful criticisms, no intentions of putting you down.
  • Adel Boutros
    Adel Boutros over 12 years
    @mKorbel Stay tuned for the Saturday Night Show then ;)
  • mKorbel
    mKorbel over 12 years
    @Adel Boutros right but I'll split my body to the 3-6th pieces on FRI, and basically I need two days for put these parts together