• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

2D Array Trouble

 
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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!
 
Ranch Hand
Posts: 346
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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...
 
Ranch Hand
Posts: 1646
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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++.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic