user2041920
user2041920

Reputation: 147

How to use this boolean in an if statement?

private String getWhoozitYs(){
    StringBuffer sb = new StringBuffer();
    boolean stop = generator.nextBoolean();
    if(stop = true)
    {
        sb.append("y");
        getWhoozitYs();
    }
    return sb.toString();
}

This is a chunk of code for a project I'm doing in a programming course. The problem I'm having is that after declaring the boolean stop and trying to assign a randomly generated boolean value to it, I can't use it in the if statement to determine if I should append more y's to the StringBuffer or not. I do have the Random generator inside a constructor, so that part isn't a problem. I assumed that since I declared the boolean outside the if statement I would be able to use it inside, but that doesn't seem to be the case. The real question is how can I use a randomly determined boolean in an if statement.

Upvotes: 11

Views: 139986

Answers (8)

Averroes
Averroes

Reputation: 4228

The problem here is

if (stop = true) is an assignment not a comparison.

Try if (stop == true)

Also take a look at the Top Ten Errors Java Programmers Make.

Upvotes: 5

Maroun
Maroun

Reputation: 95958

if(stop = true) should be if(stop == true), or simply (better!) if(stop).

This is actually a good opportunity to see a reason to why always use if(something) if you want to see if it's true instead of writing if(something == true) (bad style!).

By doing stop = true then you are assigning true to stop and not comparing.

So why the code below the if statement executed?

See the JLS - 15.26. Assignment Operators:

At run time, the result of the assignment expression is the value of the variable after the assignment has occurred. The result of an assignment expression is not itself a variable.

So because you wrote stop = true, then you're satisfying the if condition.

Upvotes: 27

Vincent van der Weele
Vincent van der Weele

Reputation: 13177

Actually, the entire approach would be cleaner if you only had to use one instance of StringBuffer, instead of creating one in every recursive call... I would go for:

private String getWhoozitYs(){
     StringBuffer sb = new StringBuffer();
     while (generator.nextBoolean()) {
         sb.append("y");
     }

     return sb.toString();
}

Upvotes: 3

Achintya Jha
Achintya Jha

Reputation: 12843

if(stop == true)

or

if(stop)

= is for assignment.

== is for checking condition.

if(stop = true) 

It will assign true to stop and evaluates if(true). So it will always execute the code inside if because stop will always being assigned with true.

Upvotes: 2

MondayPaper
MondayPaper

Reputation: 1579

additionally you can just write

if(stop)
{
        sb.append("y");
        getWhoozitYs();
}

Upvotes: 1

mp5
mp5

Reputation: 159

= is for assignment

write

if(stop){
   //your code
}

or

if(stop == true){
   //your code
}

Upvotes: 1

Rahul Tripathi
Rahul Tripathi

Reputation: 172438

Try this:-

private String getWhoozitYs(){
    StringBuffer sb = new StringBuffer();
    boolean stop = generator.nextBoolean();
    if(stop)
    {
        sb.append("y");
        getWhoozitYs();
    }
    return sb.toString();
}

Upvotes: 1

CloudyMarble
CloudyMarble

Reputation: 37566

Since stop is boolean you can change that part to:

//...
if(stop) // Or to: if (stop == true)
{
   sb.append("y");
   getWhoozitYs();
}
return sb.toString();
//...

Upvotes: 1

Related Questions