# 2D Array Help Please

• 12-07-2011, 07:01 PM
prognewb
2D Array Help Please
Hey everyone, I'm new to this forum. This is my first semester of programming and I'm working on a project. I was hoping someone could take a look at my code and help me figure out what I'm doing wrong.

Note: The program compiles just fine, however, one of the operations isn't performing correctly on output.

I have a program that is a 2 dimensional array (4 x 3). It holds numbers that the user can enter. I have to create 3 functions. On the first function I have get to pass the array into the method and then get the total of all of the numbers that were put into the array. I did that fine, no problem.

The second method is to pass the array into it and then get the average for all of the numbers. I did that no problem as well.

The third method is where my problem comes. I am supposed to get the total of a row, however, the user gets to choose which row that is. This method has to have two arguments, one for the array, of course, and then one for the users choice of the row. In the main method I collected input to total the row that the user wanted totaled. It doesn't work correctly though. It totals up the first row of numbers every time and displays that. I don't know how to control it I guess you would say.

I would really appreciate it if someone could take a look at this and help me out. Thanks.

CODE:

Code:

```import java.util.Scanner; import java.text.DecimalFormat; /**         This program will include a two-dimensional array to hold test data and perform         mathematical operations on them. */ public class testData {         public static void main(String[] args)         {                 final int ROWS = 4;                        //4 rows of test data                 final int COLS = 3;                        //3 columns of test data                 double totalGrade = 0.0;                //Accumulator                 int total = 0, cc;                                 //Create an array to hold the test data for each                 //row.                 double[][] testData = new double[ROWS][COLS];                                 //Create a Scanner object for keyboard input.                 Scanner keyboard = new Scanner(System.in);                                 //Display an introduction.                 //System.out.println("Introduction");                                                 //Nested loops to fill the array with test data                 for (int row = 0; row < ROWS; row++)                 {                         for (int col = 0; col < COLS; col++)                         {                                 System.out.println("Row " + (row + 1) +                                                                                 ", Column " + (col + 1) +                                                                                 ": ");                                 testData[row][col] = keyboard.nextDouble();                         }                                         }                 System.out.println("What row do you want the total for? (1,2,3 or 4): ");                 cc = keyboard.nextInt();                 System.out.println();                //Print blank line.                 System.out.println("The total grade is: " + getTotal(testData));                 System.out.println("The average grade is: " + getAverage(testData));                 System.out.println("The total of the rows is: " + getRowTotal(testData, total));         }         //getTotal Method         public static double getTotal(double[][] testData)         {                 double totalGrade = 0.0;                         //Nested loops to add all the elements of the array.                 for (int row = 0; row < testData.length; row++)                 {                         for (int col = 0; col < testData[row].length; col++)                         {                                 totalGrade += testData[row][col];                         }                 }                 return totalGrade;         }         //getAverage Method         public static double getAverage(double[][] testData)         {                 double totalGrade = 0.0;                 double averageGrade = 0.0;                 double totalLength = 0.0;                 double rows = 4;                 double cols = 3;                         //Nested loops to add all the elements of the array.                 for (int row = 0; row < testData.length; row++)                 {                         for (int col = 0; col < testData[row].length; col++)                         {                                 totalGrade += testData[row][col];                                 totalLength = rows * cols;                                 averageGrade = totalGrade/totalLength;                         }                                         }                 return averageGrade;         }                 //getRowTotal Method         public static double getRowTotal(double[][] testData, int cc)         {                 double totalRow = 0.0;                 int cols = 0;                                 for (int row = 0; row < testData[0].length; row++)                 {                         totalRow += testData[cc][row];                 }                 return totalRow;         } }```
• 12-07-2011, 07:20 PM
joeyvitoro
Re: 2D Array Help Please
Add System.out.println(cc+" "+row) to your for loop. Take a look at what you're actually printing.
• 12-07-2011, 07:34 PM
Norm
Re: 2D Array Help Please
Where do you call the method? What are the values of the arguments that you pass to the method?
Print them out so you see what they are.