11
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.

user2041920
  • 147
  • 1
  • 2
  • 6

8 Answers8

27

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.

Maroun
  • 94,125
  • 30
  • 188
  • 241
  • 4
    Actually, it should just be `if (stop)`. That's not even open to misinterpretation due to typos. :P – cHao Mar 14 '13 at 15:04
  • 1
    the if is still comparing, it's just since stop gets set to true, it will always evaluate to true. sorry this is a nitpick but it seems like something worth explaining (since the OP is a beginner). – Nathan Hughes Mar 14 '13 at 15:07
  • One doubt... `if(stop)` and `if(stop == true)` does second one need extra cpu cycles for execution. Programatically both interprete same, then why is it bad to use? – kAmol Jan 10 '17 at 08:35
  • 1
    @unknown No complexity difference between the two. It's just like asking "Is it true that the variable is true" instead of "Is the variable true". – Maroun Jan 10 '17 at 08:36
  • @MarounMaroun yes but at cpu instruction level I doubt that is the case. Because "is it true that the variable is true" has 2 conditions I think. Do you have any document or something for it? – kAmol Jan 11 '17 at 06:58
  • And `if(stop)` can also be written as `if(((stop==true)==true)==true)` ? I mean won't it affect? I'm not asking for code complexity, but for cpu cyle execution level. Just for why is it bad style? – kAmol Jan 11 '17 at 07:01
  • @unknown I think that most compilers will optimize this. – Maroun Jan 11 '17 at 11:24
  • for me `if(stop)` doesnt seem to work. However, if I do `if(stop == true)`, it works. Any thoughts? – Obaid Maroof Mar 30 '17 at 17:04
  • @obaidmaroof no way.. please check again. – Maroun Mar 30 '17 at 18:50
5

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.

mirekphd
  • 4,799
  • 3
  • 38
  • 59
Averroes
  • 4,168
  • 6
  • 50
  • 63
3

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();
}
Vincent van der Weele
  • 12,927
  • 1
  • 33
  • 61
2
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.

Achintya Jha
  • 12,735
  • 2
  • 27
  • 39
1

Since stop is boolean you can change that part to:

//...
if(stop) // Or to: if (stop == true)
{
   sb.append("y");
   getWhoozitYs();
}
return sb.toString();
//...
CloudyMarble
  • 36,908
  • 70
  • 97
  • 130
1

Try this:-

private String getWhoozitYs(){
    StringBuffer sb = new StringBuffer();
    boolean stop = generator.nextBoolean();
    if(stop)
    {
        sb.append("y");
        getWhoozitYs();
    }
    return sb.toString();
}
Rahul Tripathi
  • 168,305
  • 31
  • 280
  • 331
1

= is for assignment

write

if(stop){
   //your code
}

or

if(stop == true){
   //your code
}
mp5
  • 159
  • 1
  • 11
1

additionally you can just write

if(stop)
{
        sb.append("y");
        getWhoozitYs();
}
MondayPaper
  • 1,569
  • 1
  • 15
  • 20