2 Replies - 557 Views - Last Post: 05 October 2012 - 04:53 PM Rate Topic: -----

#1 DJ1one  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 13
  • Joined: 05-October 12

Error when adding values in 2D Array

Posted 05 October 2012 - 02:26 PM

I am trying to add all the numbers in a 2D array. These numbers have to be inputted. I have gotten the program to compile, but I keep getting the ArrayIndexOutofBounds: 4 error. I think the problem lies within the getData method, I have tried all I can in changing the code in that method, but to no avail. Here is the code so far:
import java.util.Scanner;

public class Lab3 {

// Get sum of all numbers in array method
public static int getSum(double[][] list) {
	list = new double[4][4];
	int sum = 0;
	int r,c = 0;
	for (r = 0; r < list.length; r++)
		for (c = 0; c < list[r].length; c++)
			sum += list[r][c];
	return sum;		

	}
// Input data method
public static double[][] getData(double[][] list) {
	Scanner input = new Scanner(System.in);
	double[][] data = new double[list.length][list[0].length];
	int r,c = 0;
	double num = 0;
	for (r = 0; r < data.length; r++)
		for (c = 0; c < data[r].length; c++)
			num = input.nextDouble();
			data[r][c] = num;
	
	return data;
	}
			

// Main method
public static void main(String[] args) {
	System.out.println("Enter a 4-by-4 matrix row-by-row:");
	double[][] numList = new double[4][4];
	double[][] newList;
	newList = getData(numList);
	int sum = getSum(newList);
	System.out.println("Sum of the matrix is " + sum);
	}
	
}
	


When i try running the program I get this :
Enter a 4-by-4 matrix row-by-row:
1 2 3 4
5 6 7 8
9 10 11 12
13 14 15 16
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 4
	at Lab3.getData(Lab3.java:25)
	at Lab3.main(Lab3.java:36)


Any help will be much appreciated as this has given me a headache...

Is This A Good Question/Topic? 0
  • +

Replies To: Error when adding values in 2D Array

#2 Maynia  Icon User is online

  • New D.I.C Head

Reputation: 9
  • View blog
  • Posts: 48
  • Joined: 17-March 09

Re: Error when adding values in 2D Array

Posted 05 October 2012 - 03:42 PM

Watch your bracketing. The inner for loop here:

	for (r = 0; r < data.length; r++)
		for (c = 0; c < data[r].length; c++)
			num = input.nextDouble();
			data[r][c] = num;		


is only executing the line
num = input.nextDouble();

each time it runs. This line:
data[r][c] = num;

is only executed once, with values 4,4. This cell doesn't exist in a 4x4 array, whose furthest limit is [3][3].

This post has been edited by Maynia: 05 October 2012 - 03:47 PM

Was This Post Helpful? 1
  • +
  • -

#3 DJ1one  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 13
  • Joined: 05-October 12

Re: Error when adding values in 2D Array

Posted 05 October 2012 - 04:53 PM

View PostMaynia, on 05 October 2012 - 03:42 PM, said:

Watch your bracketing. The inner for loop here:

	for (r = 0; r < data.length; r++)
		for (c = 0; c < data[r].length; c++)
			num = input.nextDouble();
			data[r][c] = num;		


is only executing the line
num = input.nextDouble();

each time it runs. This line:
data[r][c] = num;

is only executed once, with values 4,4. This cell doesn't exist in a 4x4 array, whose furthest limit is [3][3].


THANKS!!!!!! :batman:
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1