0

I want to write a program that creates a two-dimensional int array initialized with test data. The program cannot run. I am confused about the code.Please help with the problem.How to correct my code? Thank you.

    public class Int2DArray {

        private static int x;
        private static int y;





        public static int getTotal(int[][] numbers) {
            int total = 0;
            for (int x = 0; x < numbers.length; x++);
            for (int y = 0; y < numbers[x].length; y++);
            total = total + numbers[x][y];
            return total;
        }

        public static double getAverage(int[][] numbers) {
            double average = 0;
            average = getTotal(numbers) / (x + y);
            return average;
        }

        public static int getRowTotal(int[][] numbers, int index) {
            int total = 0;
            for (int y = 0; y < 3; y++) {
                total = total + numbers[index][y];
            }
            return total;
        }

        public static int getColumnTotal(int[][] numbers, int index) {
            int total = 0;
            for (int x = 0; x < numbers.length; x++) {
                total = total + numbers[x][index];
            }
            return total;
        }

        public static int getHighestInRow(int[][] numbers, int x) {
            int high = numbers[x][0];
            for (int i = 1; i < 3; i++) {
                if (numbers[x][i] > high) {
                    high = numbers[x][i];
                }
            }
            return high;
        }

        public static int getLowestInRow(int[][] numbers, int x) {
            int low = numbers[x][0];
            for (int i = 1; i < 3; i++) {
                if (numbers[x][i] < low) {
                    low = numbers[x][i];
                }
            }
            return low;
        }


    }
  public class Int2DArrayTest {

    public static void main(String[] args) {
        int[][] iarray = {{2, 1, 9}, {7, 3, 4}, {5, 6, 8}};
        System.out.println("Total:" + getTotal(iarray));
        System.out.println("Average:" + getAverage(iarray));
        System.out.println("Total of row 0" + getRowTotal(iarray, 0));
        System.out.println("Total of row 1" + getRowTotal(iarray, 1));
        System.out.println("Total of row 2" + getRowTotal(iarray, 2));
        System.out.println("Total of col 0" + getColumnTotal(iarray, 0));
        System.out.println("Total of col 1" + getColumnTotal(iarray, 1));
        System.out.println("Total of col 2" + getColumnTotal(iarray, 2));
        System.out.println("Highest in row 0" + getHighestInRow(iarray, 0));
        System.out.println("Highest in row 1" + getHighestInRow(iarray, 1));
        System.out.println("Highest in row 2" + getHighestInRow(iarray, 2));
        System.out.println("Lowest in row 0" + getLowestInRow(iarray, 0));
        System.out.println("Lowest in row 1" + getLowestInRow(iarray, 1));
        System.out.println("Lowest in row 2" + getLowestInRow(iarray, 2));
    }
}
3
  • 4
    Do you have compile or logical error? Commented Mar 9, 2014 at 20:14
  • 2
    Also check your getTotal method, specially the semi colons after each for. Commented Mar 9, 2014 at 20:16
  • you have also division by 0 in getAverage (x,y is not initialized) Commented Mar 9, 2014 at 20:19

3 Answers 3

2

I see one big error:

you can resolve it so:

public static int getTotal(int[][] numbers) {
            int total = 0;
            for (int x = 0; x < numbers.length; x++);
            for (int y = 0; y < numbers[x].length; y++);
            total = total + numbers[x][y];
            return total;
        }

replace for

      public static int getTotal(int[][] numbers) { 
            int total = 0;
            for (int x = 0; x < numbers.length; x++){
               for (int y = 0; y < numbers[x].length; y++){
                  total = total + numbers[x][y];
              }
            }
            return total;
        }

or better so(using foreach):

public static int getTotal(int[][] numbers) {
        int total = 0;
        for (int [] x : numbers){
            for (int y : x){
                total = total + y;
            }
        }
        return total;
    }

And use formatting hotkey in your IDE.


public static int getRowTotal(int[][] numbers, int index) {
            int total = 0;
            for (int y = 0; y < 3; y++) {
                total = total + numbers[index][y];
            }
            return total;
        }

here you use constant 3 - it is bad style you need to extract it as constant


public static double getAverage(int[][] numbers) {
            double average = 0;
            average = getTotal(numbers) / (x + y);
            return average;
        }
  1. x and y are not initialized in your code
  2. why do you divide by x+y. You need divide on count of element.
Sign up to request clarification or add additional context in comments.

Comments

2

You have declared the methods inside Int2DArray as static

public static int getTotal(int[][] numbers) { //

So you need to call them as static in order to use them like:

System.out.println("Total:" + Int2DArray.getTotal(iarray));

1 Comment

Thank you.Now I know how to use the methods.
0

You are having following line twice, so an obvious syntactical error.

 public class Int2DArrayTest {

Moreover, you also have logical error in getAverage function.

average = getTotal(numbers) / (x + y);

It shows divide by zero exception.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.