File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes Code is not working Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Code is not working" Watch "Code is not working" New topic
Author

Code is not working

john price
Ranch Hand

Joined: Feb 24, 2011
Posts: 495


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?


“Don’t worry if it doesn’t work right. If everything did, you’d be out of a job.” (Mosher's Law of Software Engineering)
“If debugging is the process of removing bugs, then programming must be the process of putting them in.” (Edsger Dijkstra)
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41478
    
  51
if (started = false) {

This line is missing one crucial character.


Ping & DNS - my free Android networking tools app
john price
Ranch Hand

Joined: Feb 24, 2011
Posts: 495

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

Joined: Oct 24, 2010
Posts: 557
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.


Learning Java using Eclipse on OpenSUSE 11.2
Linux user#: 501795
Darryl Burke
Bartender

Joined: May 03, 2008
Posts: 4527
    
    5

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.


luck, db
There are no new questions, but there may be new answers.
David O'Meara
Rancher

Joined: Mar 06, 2001
Posts: 13459

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

Joined: Feb 17, 2011
Posts: 21

nice thread.. i've learned a lot..
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11229
    
  16

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.


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38334
    
  23
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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Code is not working