Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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));
    }
}
share|improve this question
4  
Do you have compile or logical error? –  gstackoverflow Mar 9 at 20:14
2  
Also check your getTotal method, specially the semi colons after each for. –  ZouZou Mar 9 at 20:16
    
you have also division by 0 in getAverage (x,y is not initialized) –  gawi Mar 9 at 20:19

3 Answers 3

up vote 2 down vote accepted

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.
share|improve this answer

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));
share|improve this answer
    
Thank you.Now I know how to use the methods. –  user3375421 Mar 9 at 20:20
    
@user3375421 You're Welcome, and please accept it as answer if that solved your problem. –  Salah Mar 9 at 20:23

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.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.