1 Replies - 1167 Views - Last Post: 09 December 2012 - 06:55 PM Rate Topic: -----

#1 mrsWhit1278  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 09-December 12

2D Array Operations error

Posted 09 December 2012 - 05:53 PM

OK this is an assignment due at 11:30pm tonight. I have very little time left. I am getting this when I try to run it.
java.lang.ArrayIndexOutOfBoundsException: 3
at ArrayOps.getTotal<ArrayOps.java:55>
at ArrayOps.main<ArrayOps.java:20>
The debug is not picking anything up so I don't know what to change. I have tried everything I know to try. I haven't been able to talk to my professor but once this week.
The spacing and what not gets unorganized when you paste it so ignore anything that is over too far or vise versa.
private static int getTotal(int[][] numbers)
	{
		int total = 0;		//acummulator	
		for (int row = 0; row < numbers.length; row++)
  	   {
			for (int col = 0; col < numbers.length; col++)
				total += numbers[row][col];
     	} 
		return total;
	}
		
	/* The following method is to return the average of the numbers int the
   	array as the arrayAverage variable
	*/
		
	private static double getAverage(int[][] numbers)
	{
		double average;
		int total = 0;		//acummulator	
		for (int row = 0; row < numbers.length; row++)
  	   {
			for (int col = 0; col < numbers.length; col++)
			{
				total += numbers[row][col];
			}
     	} 
		average = total / numbers.length;
		return average;
   }		
	/* The following method is to get and display the total for one row in the 
		numbers array as rowTotal
	*/	
		 
	private static int getRowTotal(int[][] numbers)
	{	
		int rowTotal = 0;	//accumulator
				
      for (int row = 0; row < numbers.length; row++) 
      {
			for (int col = 0; col < numbers[row].length; col++)
				rowTotal += numbers[row][col];
			System.out.println("The total for row " + row + " is " + rowTotal);	
		}
		return rowTotal;  
  	}
	
	/* The following method is to get and display the total for one column in the 
		numbers array as colTotal
	*/
		
	private static int getColumnTotal(int[][] numbers)
	{
   	int colTotal = 0; //accumulator
      for (int col = 0; col < numbers[0].length; col++)
      {
			for (int row = 0; row < numbers.length; row++)    
		   	colTotal += numbers[row][col];
		System.out.println("The total for column " + col + " is " + colTotal);
		}
		return colTotal;
	}
		
	/* The following method is to get and the highest number in a given row
		in the numbers arrat as rowHighest
	*/
		
	private static int getHighestInRow(int[][] numbers)
	{ 
		//loop for getting highest number
   	int highest = numbers[0][0];
      for (int row = 0; row < numbers[row].length; row++)
      {
      	if (numbers[row][0] >= highest)
         	highest = numbers[row][0];
			System.out.println("The highest number in row " + row + " is " + highest);
		}
		return highest;
	}
		
	/* The following method is to return the lowest number in a given row
		in the numbers arrat as rowLowest
	*/
		
	private static int getLowestInRow(int[][] numbers)
	{	
		int lowest = numbers[0][0];  
		for (int row = 0; row < numbers[row].length; row++)
		{
        	if (numbers[row][0] <= lowest)
				lowest = numbers[row][0];
			System.out.println("The lowest number in row " + row + " is " + lowest);
		}
		return lowest;
	}
}



Is This A Good Question/Topic? 0
  • +

Replies To: 2D Array Operations error

#2 k3y  Icon User is offline

  • D.I.C Head

Reputation: 36
  • View blog
  • Posts: 205
  • Joined: 25-February 12

Re: 2D Array Operations error

Posted 09 December 2012 - 06:55 PM

For your inner loops, I believe you have to change it to
for (int col = 0; col < numbers[row].length; col++)


Also to find the highest and lowest element of an array you can use
Arrays.sort(array_name);


For highest
System.out.print(array[array.length-1]);


For lowest
System.out.print(array[0]);


Now you have to figure out how to apply that to a 2 Dimensional array. I'm sure google can assist there

This post has been edited by k3y: 09 December 2012 - 06:56 PM

Was This Post Helpful? 1
  • +
  • -

Page 1 of 1