[java] 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.

This question is related to java if-statement boolean

The answer is


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.


The problem here is

if (stop = true) is an assignation not a comparision.

Try if (stop == true)

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


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.


Try this:-

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

Since stop is boolean you can change that part to:

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

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

= is for assignment

write

if(stop){
   //your code
}

or

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

additionally you can just write

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

Examples related to java

Under what circumstances can I call findViewById with an Options Menu / Action Bar item? How much should a function trust another function How to implement a simple scenario the OO way Two constructors How do I get some variable from another class in Java? this in equals method How to split a string in two and store it in a field How to do perspective fixing? String index out of range: 4 My eclipse won't open, i download the bundle pack it keeps saying error log

Examples related to if-statement

How to use *ngIf else? SQL Server IF EXISTS THEN 1 ELSE 2 What is a good practice to check if an environmental variable exists or not? Using OR operator in a jquery if statement R multiple conditions in if statement Syntax for an If statement using a boolean How to have multiple conditions for one if statement in python Ifelse statement in R with multiple conditions If strings starts with in PowerShell Multiple conditions in an IF statement in Excel VBA

Examples related to boolean

Convert string to boolean in C# In c, in bool, true == 1 and false == 0? Syntax for an If statement using a boolean Truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all() Ruby: How to convert a string to boolean Casting int to bool in C/C++ Radio Buttons ng-checked with ng-model How to compare Boolean? Convert True/False value read from file to boolean Logical operators for boolean indexing in Pandas