# Thread: for loop help

1. Member
Join Date
Oct 2008
Posts
39
Rep Power
0

## for loop help

What is wrong with my for loop, i am calling on a method and if x=5 then the program will close? keep getting the error "(" expected

public static void clearPlayingField() {

for Tron.ClearGridPoint (x=5){

Tron.MAX_DIM;
}// for( x=.....

2. that's not how you write a for loop.

3. Member
Join Date
Oct 2008
Posts
39
Rep Power
0
Thank you, any idea how i would edit that if i wanted to use a method in it??

4. how does Tron.ClearGridPoint (x=5) look like? what does it return?

5. Member
Join Date
Oct 2008
Posts
39
Rep Power
0
Below is the method, i am trying to clear the grid if x =5?
(a bit like the snake game on the old nokia phones)

public static void ClearGridPoint(int x, int y) {

grid[x][y]=0;

if((x==0)&&(y==0)) {
System.out.println("Upper left of grid cleared");
}
if((x==(MAX_DIM-1))&&(y==(MAX_DIM-1))) {
System.out.println("Lower right of grid cleared");
}

6. then why don't you just call Tron.ClearGridPoint (x=5). why must you put it in a for loop??

you can do this:
if(x==5) {
Tron.ClearGridPoint(x,y);
}

7. Member
Join Date
Oct 2008
Posts
39
Rep Power
0
Thats how i understood what i needed to do for my assigmment, i have 5 methods and the one i'am stuck on is required below, do you think i have misunderstood something?

Five Methods to Edit
Method : ClearPlayingField()
The game needs this method to clear-up the playing matrix at startup. Inside this method implement a
'for-loop-in-a-foor-loop' structure to clear the playing grid using the Tron.ClearGridPoint( x , y
); method. If, for example, x holds the value 5 and y the value 6, the grid coordinates [5,6] will
be cleared. As both 'for' loops run from zero to Tron.MAX_DIM, eventually all grid coordinates will
be cleared.

8. i'm guessing you need something like this:
Java Code:
```for(int i = 0; ...){
for(int j = 0; ...){
Tron.ClearGridPoint( x , y );
}
}```
* fill in the ... using this: "both 'for' loops run from zero to Tron.MAX_DIM"

remember: if the thread is solved, mark it as solved.
Last edited by angryboy; 01-28-2009 at 03:43 PM.

9. Soc... you can't just throw code together and hope it works... Here's some basics of "for" loops:
for ( {initialization}; {exit condition}; {incrementor} )
{code_block;}
For example, to execute a simple loop 5 times printing out the iteration number each time through the loop:
Java Code:
```for ( i=1; i<6; i++ ) {
System.out.println(i);
}```
*courtesy of tech-recipes
The for Statement (The Java™ Tutorials > Learning the Java Language > Language Basics)

I would strongly suggest that you use the following link for further basic Java questions:

The Java™ Tutorials

Luck,
CJSL
Last edited by CJSLMAN; 01-28-2009 at 04:03 PM. Reason: Add link

10. Member
Join Date
Jan 2011
Posts
14
Rep Power
0
EDIT: ignore this
Last edited by HaplessNoob; 01-25-2011 at 01:43 PM.

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•