File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Java in General and the fly likes Problem with For Loop? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Problem with For Loop?" Watch "Problem with For Loop?" New topic

Problem with For Loop?

Thomas Crew

Joined: Nov 13, 2012
Posts: 10
I'm trying to have each shape that's in the array move around the canvas randomly and independently. When the shape (array[i]) goes out of the canvas, it should be reset in another random location to continue moving once again. Can anyone tell me what's wrong with this code?

The "Driver" class draws the shapes from the class Star on to the canvas.

fred rosenberger
lowercase baba

Joined: Oct 02, 2003
Posts: 11955

That is a lot of code to expect someone to go through, and not much info on where to look. Can you simply/reduce the amount of code you post but still demonstrate the issue? Preferably one we can compile and run as-is.

Generally speaking, when stuff isn't working how I think it should, I start sticking in System.out.println() statement. Put them in at the top and bottom of each method telling me that I am entering/leaving. Put them in each time you change a variable. Put them in at the top of a loop. Put them in before each IF statement, or in each block of the If/else if/else block.

eventually, you can narrow down where the problem is, and figure out why something isn't changing that should, or is changing that shouldn't.

There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Rajdeep Biswas
Ranch Hand

Joined: Mar 26, 2012
Posts: 187

Whenever theres trouble, debug it.
If you a beginner, you may take my recommendation
If you are on console, go for System.out.println statements as Fred insisted, and if you're with an IDE, add debug points to important locations and run the program in debug mode, and see that the variables are having what values! And you will be on track again!
And yes, this big code is very hard to read in a short span, so its recommended for OP to show some research effort and narrow down the question to specific issues!
I agree. Here's the link:
subject: Problem with For Loop?
It's not a secret anymore!