private String getWhoozitYs(){
StringBuffer sb = new StringBuffer();
boolean stop = generator.nextBoolean();
if(stop = true)
{
sb.append(\"y\
Since stop
is boolean you can change that part to:
//...
if(stop) // Or to: if (stop == true)
{
sb.append("y");
getWhoozitYs();
}
return sb.toString();
//...
Try this:-
private String getWhoozitYs(){
StringBuffer sb = new StringBuffer();
boolean stop = generator.nextBoolean();
if(stop)
{
sb.append("y");
getWhoozitYs();
}
return sb.toString();
}
additionally you can just write
if(stop)
{
sb.append("y");
getWhoozitYs();
}
= is for assignment
write
if(stop){
//your code
}
or
if(stop == true){
//your code
}
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)
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.