• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Code is not working

 
john price
Ranch Hand
Posts: 495
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Output:

I did this several times and came up with the same result. What is wrong with my code and how do I fix it?
 
Ulf Dittmer
Rancher
Posts: 42968
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
if (started = false) {

This line is missing one crucial character.
 
john price
Ranch Hand
Posts: 495
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ulf Dittmer wrote:
if (started = false) {

This line is missing one crucial character.


Thank you very much. Why didn't I get an error message from this?
I was missing the "extra" =.
Thanks,
cc11rocks
 
Greg Brannon
Bartender
Posts: 563
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Some IDEs will flag this as a possible error, but, technically, it's not an error. In the if statement,

if ( x )

x is evaluated as either true or false. In your code, if ( started = false ), in the expression

started = false;

false was assigned to started, and

if ( started )

was evaluated as being either true or false. In this case, I assume it was determined to be false.
 
Darryl Burke
Bartender
Posts: 5132
11
Java Netbeans IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Also, it's bad form to use == to compare booleans to true or false.
And the topic has nothing to do with GUIs, so I shall move it to an appropriate place.
 
David O'Meara
Rancher
Posts: 13459
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Or get in the habit of using Yoda Conditions
if(false=started) will give the error you expect
if(false==started) will then work as desired
see alse
if("".equals(mystring)) and other constant==variable forms.
 
Death Arte
Greenhorn
Posts: 21
Eclipse IDE Java Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
nice thread.. i've learned a lot..
 
fred rosenberger
lowercase baba
Bartender
Posts: 12145
30
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
One tip on posting questions...When you simply say "My code doesn't work", it can be extremely hard for folks to know what you mean. Please tell what is is doing (you did that) and what you think it should be doing (that's the part you didn't).

A lot of the time, the code IS working the way it should, but the original poster's idea of how it should work is wrong - and that can't be corrected unless we know what that is.
 
Campbell Ritchie
Sheriff
Pie
Posts: 49382
62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
David O'Meara wrote: . . .
if(false=started) will give the error you expect
if(false==started) will then work as desired
see also
if("".equals(mystring)) and other constant==variable forms.
I personally would ban the use of == or != with true or false as an operand, on either side. But if ("Campbell".equals(name)) is a good idea; you cannot get a NullPointerException from it, however hard you try.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic