Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Help Automatically Updating Value

 
Guy Burstein
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 34
Eclipse IDE Java Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Campbell Ritchie
Sheriff
Pie
Posts: 49733
69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 49733
69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 49733
69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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().
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic