# Sum of 2d Array Printing Garbage

Page 1 of 1

## 2 Replies - 580 Views - Last Post: 14 November 2012 - 09:59 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'https://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=300276&amp;s=6e12d139ade67a7af9d80333214c4ba2&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 Brovahkiin501

Reputation: 0
• Posts: 48
• Joined: 01-October 12

# Sum of 2d Array Printing Garbage

Posted 14 November 2012 - 09:36 PM

Hi guys! I am trying to get the total sum of a 2d array that I have created but it keeps printing as garbage. Can somebody please help?! It's quite urgent /: I'll post my code and the output!

Code:
```public class budgePlan {
public static void main(String[] args) {
final int ROWS = 5;
final int COLS = 11;
int[][] budget = new int[6][12];
for (int i = 0; i < budget[0].length; i++) {
budget[0][i] = 500;
}
for (int i = 0; i < budget[1].length; i++) {
budget[1][i] = 200 + (int)(Math.random() * 201);
}
for (int i = 0; i < budget[2].length; i++) {
budget[2][i] = 100 + (int)(Math.random() * 101);
}
for (int i = 0; i < budget[3].length; i++) {
budget[3][i] = 300 + (int)(Math.random() * 201);
}
for (int i = 0; i < budget[4].length; i++) {
budget[4][i] = 125 + (int)(Math.random() * 151);
}
for (int i = 0; i < budget[5].length; i++) {
budget[5][i] = 150 + (int)(Math.random() * 251);
}
for (int i = 0; i <= ROWS; i++) {
for (int j = 0; j <= COLS; j++) {
System.out.print(" " + budget[i][j]);
}
System.out.println("");
}
System.out.print("\nThe total amount of money you need this year is: " + moneyNeeded(budget));

}
public static int[][] moneyNeeded(int[][] budget) {
int total = 0;
for (int row = 0; row < budget.length; row++) {
for (int column = 0; column < budget[row].length; column++) {
total += budget[row][column];
}
}
return budget;

}

}

```

Output:
```500 500 500 500 500 500 500 500 500 500 500 500
347 245 330 384 210 210 365 247 239 346 252 336
115 159 153 134 151 187 132 126 101 124 141 121
405 437 408 354 497 434 413 344 426 301 373 430
154 275 217 239 213 228 150 137 187 151 260 250
375 387 254 203 342 383 332 289 207 180 368 259

The total amount of money you need this year is: [[[email protected]

```

Figured it out! Thanks all!

Is This A Good Question/Topic? 0

## Replies To: Sum of 2d Array Printing Garbage

### #2 blackcompe

• D.I.C Lover

Reputation: 1159
• Posts: 2,547
• Joined: 05-May 05

## Re: Sum of 2d Array Printing Garbage

Posted 14 November 2012 - 09:50 PM

Quote

Figured it out! Thanks all!

So you figured it out?

### #3 some_kid

Reputation: 10
• Posts: 43
• Joined: 26-May 09

## Re: Sum of 2d Array Printing Garbage

Posted 14 November 2012 - 09:59 PM

Take a close look at what you are returning in moneyNeeded it is not what it should be.