Java nested for loop - a table - looking for some help/advice

I read the faq and no luck.

Page 1 of 1

0 Replies - 2747 Views - Last Post: 03 November 2008 - 07:21 PM Rate Topic: -----

#1 fall3nelite   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 1
  • Joined: 03-November 08

Java nested for loop - a table - looking for some help/advice

Post icon  Posted 03 November 2008 - 07:21 PM

Hi I'm new to this forum but I joined today to ask a question about a nested for loop in Java that I'm making. I read the faq and couldn't find any information specifically on for loops.

So I'm trying to make a table to display various prices of a carpet at specific lengths and widths. This is what is is supposed to look like:

10 12 14 … and so on until 20 (this is length)

9 450 540 630... and so on

10 500 600 700... and so on

(right side rows are width) The values under length and width are the price of the carpet at that specific length and width.

When I compile my code and view it in the Applet in TextPad, I can see the cost getting displayed, but not the length and width values and it does not format to look like a table like it is supposed to. I spent a couple hours today trying to figure this out but I didn't get very far. I'm a beginner in Java and new to looping in Java.

Here is my code:

import java.awt.*;
import java.applet.Applet;


public class carpetPrice extends Applet
{

	public void paint (Graphics g)
	{
		carpetpriceTable(g);
	}

	public void carpetpriceTable (Graphics g)
	{
		int x=10, y=20;
		for (int l=8; l<=30; l++) //rows each line down
		{
		g.drawString(""+l,x,y);

			for (int w=10; w<=20; w+=2) //columns
			{

				float cost=l*w*5.0f;
				g.drawString(""+cost,x++,y);

			}

		x=10;
		y+=30;

		}

	}

} 

This post has been edited by fall3nelite: 03 November 2008 - 07:27 PM


Is This A Good Question/Topic? 1

Page 1 of 1