wood burning stoves 2.0*
The moose likes Beginning Java and the fly likes Nested for loop question. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Nested for loop question." Watch "Nested for loop question." New topic
Author

Nested for loop question.

Roger Weyersberg Ii
Greenhorn

Joined: Jul 19, 2012
Posts: 5
Hello. I'm writing a program for school. I have the whole thing up and running properly except for the required nested for loop. I'm trying to print a table of double's and it prints out with the proper numbers, but it prints them all twice and I'm at a loss as to why this is. Any help or guidance in the correct direction would be greatly appreciated.

This is the way it is currently printing:

minutes earnings
1 2
1 2
3 4
3 4
5 6
5 6
etc....

This is the way it's supposed to look:

minutes earnings
1 2
3 4
5 6
etc.....


This is the offensive code:

jTextArea1.setText("minutes\tearnings\n");


//loop for printing earings and time into the table
for(int row=0;row<index;row++){
for(int column=0;column<wareHouse[row].length;column++){
jTextArea1.append(wareHouse[row][0]+"\t"+wareHouse[row][1]+"\n");

I am aware that there are better ways of actually accomplishing what I'm trying to do here, however the nested for loop is required for this portion of the program. Thanks in advance.

I'm having problems making this website make the tables line up properly, but minus the tab spacing, that is what the tables should look like.
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 8186
    
  23

Roger Weyersberg Ii wrote:I'm having problems making this website make the tables line up properly, but minus the tab spacing, that is what the tables should look like.

It's difficult to tell from what you've posted, but I'd bet dollars to doughnuts that it has to do with the fact that you're treating Strings as numbers. Java has several wonderful collections, and java.util.Set is what you want in this case. Take your pairs of numbers and plough them into a Set and output it; the Set takes care of all the business of duplicates. If you need them sorted, there is Collections.sort().

General Tip: try to use the stuff that's given to you for free. If you don't know what that is for Java, have a good look through the java.lang, java.math and java.util packages before you start writing classes that have already been invented. The designers were quite clever chaps.

Winston


Isn't it funny how there's always time and money enough to do it WRONG?
Articles by Winston can be found here
Stuart A. Burkett
Ranch Hand

Joined: May 30, 2012
Posts: 679
You're not using your column value in the print statement. In your print statement you print out the values for the first and second column of your row and you do this for however many columns there are in that row.
Roger Weyersberg Ii
Greenhorn

Joined: Jul 19, 2012
Posts: 5
Stuart A. Burkett wrote:You're not using your column value in the print statement. In your print statement you print out the values for the first and second column of your row and you do this for however many columns there are in that row.


When I insert column in the print statement instead of statically assigning which column to print from it spits the same number out in each slot of the table.

so it's like:
11
22
33
44
55
66

which is why I had changed it to print from specific columns.

Junilu Lacar
Bartender

Joined: Feb 26, 2001
Posts: 4991
    
    8

The inner loop is supposed to append one column at a time, however many columns you have. The problem is that every time through the for-loop, you are appending two columns at a time.

This is why you see the same values twice. If you had three columns, you'd see them thrice. Four columns, you'd see them four times. And so on. Get it?

What's leading you astray is probably a confusion of when you should append the tab and the newline. Hint: the newline should be appended only after the last column has been appended. Also, if you have an extra tab appended to the last column in a line, who cares? It probably won't affect the formatting much.


Junilu - [How to Ask Questions] [How to Answer Questions]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Nested for loop question.