Hey guys,
So, I need to add booleans on my project for the final piece. I have all of it done but the booleans.

Basically, I checked about 10+ links on google, and none of them really explained how to do booleans.

So, what I need is something that does this:

if (paybackYears <= TEN)
        {
                boolean buyIt = true;

        System.out.println("Therefor, the likelyhood of you making your money back in 10 years is " + buyIt".");
        }
 else
        {
            System.out.println("Therfore, the likelyhood of you making your money back in 10 years is " + buyIt+".");
 }

I know this is kinda literal, so it probably isn't right at all, I cant figure out how I'd make it right.

Basically I want it to say false depending if paybackYears is greater than TEN, and true if paybackYears is less than TEN. (TEN = constant variable, which is 10 lol).

Anyone can explain this? I don't really want the actual answer unless you explain how to do it with it. I need to learn this but I dont have the class until monday, and I'd like to get this done before than.

Thanks,
Justin W.

If you want the boolean to be accesible in both the if and the else statement, you need to define it above them. Just define it as

boolean buyIt;//make sure you look up scope of variables

It defaults to false, so no need to initialise. Now, all you need to do, is set it to either true or false, where ever you wish. So, in the if statement, you want to print true, if the value is <= 10. Therefore, once inside that if block, just simple set buyIt = true;...The print command will then simple print out the value of buyIt, which is true. Which is what you want. Now just do the same thing in the if statement but set buyIt = false;...This will then print the value of buyIt as false.

I want it to say false depending if paybackYears is greater than TEN

Take that statement and extract the variables and the comparison operator and make an expression. Set a boolean to the value of the expression.
For example:
boolean aIsGreater = (A > B); // true if A is greater than B
boolean aIsLess = (A < B); // false if A is greater than B

So then how would you bring that up after:

System.out.println("Therefor, the likelyhood of you making your money back in 10 years is " HERE;

I put

boolean buyIt = (paybackYears<=TEN);
        boolean noBuyIt = (paybackYears>=TEN);

Thanks for the help guys.

You only need 1 boolean. It's either greater than or less than, true or false. Read my above post. :)

boolean buyIt = (paybackYears<=TEN);
        boolean noBuyIt = (paybackYears>=TEN);

Both variables will be true if the number of years is TEN. You should remove the = for one of them. Then you don't need two variables. You can use !buyIt for the noBuyIt condition.

Member Avatar for coil

A more general explanation of booleans:

Booleans are primitives. They carry one of two values: true or false, and they default to false.

You can create booleans by assigning them a value:
boolean b=true;

Or, by giving them a condition (like the ones in if-statements):
boolean b= (foo>bar);

You can use booleans directly in if-statements:
boolean b;
if(b)
...

You can get the opposite of a boolean with the !.
boolean b=true;
System.out.println(!b); //prints out false

Hey guys, thanks so much.

Didn't know they worked like that, kinda surprised to be honest.

All of you have a great night and weekend.

Thanks!

@justin

BTW, if your question gets answered, please ensure that you mark the thread as solved (this time I've done it for you). Think of this as a way of saying thanks to those who have helped you out plus making it easier for others to stumble upon your solution.

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.