File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Beginning Java and the fly likes Another nested loop question... Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Head First Android this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Another nested loop question..." Watch "Another nested loop question..." New topic

Another nested loop question...

Adam Polak

Joined: Oct 28, 2002
Posts: 23
I am trying to create an aplet wich will show rows of 5 boxes underneath eachother...I have been trying to do this with a nested loop but somehow the second set of five boxes doesn't start a new row...can someone please help me out with this?
Here is the script:
import java.awt.*;
import java.applet.Applet;
public class Tekenvelden extends Applet {

final int veldBreedte = 30;
final int veldHoogte = 30;
public void paint(Graphics g) {

tekenVeld(g, 10, 10, 15);
private void tekenVeld(Graphics g, int x, int y, int aantal){

int box= 0;
int row= 0;
int rows= (aantal/ 5);

for (row = 0; row < rows; row++) {

for (box= 0; box < aantal; box++) {
g.drawRect(x+(veldBreedte*box), y, veldBreedte, veldHoogte);
g.drawString(" "+(box+1), x+(veldBreedte*box)+veldBreedte/2, y+veldHoogte/2);


y= y + veldHoogte;



Marilyn de Queiroz

Joined: Jul 22, 2000
Posts: 9056

aantal = 15 so in your inner for loop you are telling it to print 15 boxes per line. Change it to box < 5 and you'll get what you want.
[ December 01, 2002: Message edited by: Marilyn de Queiroz ]

"Yesterday is history, tomorrow is a mystery, and today is a gift; that's why they call it the present." Eleanor Roosevelt
Adam Polak

Joined: Oct 28, 2002
Posts: 23
It makes 3 rows of 5 boxes now, but I have another problem, the numbers in the boxes don't count through. Each row has numbers one 1-5, while row one should have 1-5, row two should have 6-10 and row three should have 10-15.
William Barnes
Ranch Hand

Joined: Mar 16, 2001
Posts: 986

Add a separate counter which is incremented inside the second loop. That counter will contain the value you are looking for.

Please ignore post, I have no idea what I am talking about.
I agree. Here's the link:
subject: Another nested loop question...
It's not a secret anymore!