This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes Help Automatically Updating Value Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Help Automatically Updating Value" Watch "Help Automatically Updating Value" New topic
Author

Help Automatically Updating Value

Guy Burstein
Greenhorn

Joined: Aug 18, 2011
Posts: 1
I've been trying to figure out how to automatically update a Boolean variable. Whenever I try to compile it however I get the error "illegal start of type". Can someone tell me where I went wrong here? Thank you.
Badal Chowdhary
Ranch Hand

Joined: Apr 26, 2011
Posts: 34

Guy,

At line 23, while loop is hanging inside the class declaration. You might want to wrap it in a method.

At line 41, there is a semicolon followed by open parenthesis.

Thanks,
Badal


Blog: http://badalchowdhary.wordpress.com/
Twitter: http://twitter.com/badalrocks
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 37884
    
  22
Welcome to the Ranch

"Illegal start ..." usually mean you are trying to use keywords like public or static inside a method. That happens if you have not balanced your {s and }s correctly.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 37884
    
  22
By the way, never write == true or == false. Not only are they poor style, but they can cause nasty errors if you write = by mistake. As I think you have already done.

It's not if (b == true) ...; it's if (b) ...
Not if (b == false) ... but if (!b) ...

Beware of using if else to assign to booleans. You can reduce your loop to isMoving = speed > 0;
Of course, you are never calling that loop from any of your methods, and the isMoving field is redundant. You always have a speed field, so you can actually dispense with the isMoving field altogether, and replace it with speed > 0 throughout.

Alternatively, try this:Now you can replace isMoving with isMoving() throughout. That would probably be better. Get rid of the isMoving field.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 37884
    
  22
Better than using printState() would be to override the toString() method. Now you can call it like thisBut you will only get a useful printout if you override toString().
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Help Automatically Updating Value
 
Similar Threads
Cannot find symbol error
It compiled but couldn't run,help me make it run
Is Java too difficult for me? Trying to learn with Head first Javabook