2 Replies - 230 Views - Last Post: 14 November 2012 - 09:59 PM Rate Topic: -----

#1 Brovahkiin501  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • 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: [[I@56e5b723



Figured it out! Thanks all!

Is This A Good Question/Topic? 0
  • +

Replies To: Sum of 2d Array Printing Garbage

#2 blackcompe  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1150
  • View blog
  • Posts: 2,528
  • 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?
Was This Post Helpful? 0
  • +
  • -

#3 some_kid  Icon User is offline

  • New D.I.C Head

Reputation: 10
  • View blog
  • 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.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1