*
The moose likes Beginning Java and the fly likes 2D Array Trouble Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "2D Array Trouble" Watch "2D Array Trouble" New topic
Author

2D Array Trouble

Amy Caine
Ranch Hand

Joined: Jan 29, 2005
Posts: 30
Hello All! I am really new here (this is my first post), and am only in my 3rd week of a Java course. I took C++ and have a little bit of other languages under my belt, so I am catching on to the main topics quite well, but I have one burning question.

I am trying to get a 2D array to print out correctly. It is for an exercise for class, and I have gotten all of it but this part. The instructor just wants different sales numbers to show up for 5 different states for 3 different items. Not a big deal. I have the data printing out, but it is all on one line. Here is the code in the loops in the print method (I would copy all of it but am working on a different machine; I got a virus on my main machine and can't get on the Internet yet):

int rowSize = productSales.length;
int colSize = productSales[0].length;

for (int row = 0; row < rowSize; row = row + 1) {
salesReport = salesReport + salesRegion[row];
for (int col = 0; col < colSize; col = col + 1) {
salesReport = salesReport + "\t" + "\t" + "\t"+moneyFormat.format(productSales[row][col]);
} // end col loop
} // end row loop

There are 4 column titles; the state (hard-coded values in a separate method), then the columns for the 3 product's sales (hard-coded as well). There are 5 states to account for. However, it prints out like this:

Illinois $10 $10 $20Indiana $30 $40 $50Michigan $10 $20 $30Ohio $60 $80 $50Wisconsin $90 $80 $50

It is supposed to look like this:
Illinois $10 $10 $20
Indiana $30 $40 $50
Michigan $10 $20 $30
Ohio $60 $80 $50
Wisconsin $90 $80 $50

With column titles and all of that other stuff.

The moneyFormat is just a defined format in the same method but outside the loop. The value of the array is returned to the main method and printed to the screen from there. I have tried using a System.out.println statement of just a space outside of the col loop but still inside the row loop but to no avail. This is just an exercise for class so it doesn't have to be handed in, but our assignment is based on it. I have gotten this far, but seem to be stuck at what may be one of the easiest things to do! Any help would be GREATLY appreciated! Thanks so much!
Damanjit Kaur
Ranch Hand

Joined: Oct 18, 2004
Posts: 346
for (int row = 0; row < rowSize; row = row + 1) {
salesReport = salesReport + salesRegion[row];
for (int col = 0; col < colSize; col = col + 1) {
salesReport = salesReport + "\t" + "\t" + "\t"+moneyFormat.format(productSales[row][col]);
} // end col loop
} // end row loop


Use this

for (int row = 0; row < rowSize; row = row + 1) {
salesReport = salesReport + salesRegion[row];
for (int col = 0; col < colSize; col = col + 1) {
salesReport = salesReport + "\t" + "\t" + "\t"+moneyFormat.format(productSales[row][col]);
} // end col loop

salesReport = salesReport+"\n";
System.out.println(salesReport);

} // end row loop
Amy Caine
Ranch Hand

Joined: Jan 29, 2005
Posts: 30
Woo Hoo! It worked! Thanks so much! See, this was an easy fix. Thanks so much for the quick help! I'll definitely be back for more of my issues. Thanks again! This site is great!! I just forgot about the "\n" feature...
David Harkness
Ranch Hand

Joined: Aug 07, 2003
Posts: 1646
Congratulations, Amy, on your first post and gettnig a quick answer. Welcome to the Ranch!

Luckily the Java designers chose to bring over \r \n \t etc from C/C++.
 
 
subject: 2D Array Trouble
 
Similar Threads
arraycopy
totaling array rows and columns
Could you please help me?
ArrayIndexOutOfBoundsException that I just can't figure out.
2D to 1D