Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Nested for loop question.

 
Roger Weyersberg Ii
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 10254
59
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Stuart A. Burkett
Ranch Hand
Posts: 679
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 7465
50
Android Eclipse IDE IntelliJ IDE Java Linux Mac Scala Spring Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic