-4

How can I call a method in the main method ?

public class Sum extends ConsoleProgram {
    public void run() {
        int[][] array = {{32, 4, 14, 65, 23, 6},
                        {4, 2, 53, 31, 765, 34},
                        {64235, 23, 522, 124, 42}};
    }

    public static int sumRow(int[][] array, int row) {
        int sum = 0;
        for(int col = 0; col < array[row].length; col++) {
             sum += array[row][col];
        }
        return sum;
    }
}

I used this option

public void run() {
    int[][] array = {{32, 4, 14, 65, 23, 6},
                    {4, 2, 53, 31, 765, 34},
                    {64235, 23, 522, 124, 42}};
    sumRow(); // and this way System.out.println(sumRow);
    
}

Expected result:

144
889
64946⏎

Actual result:

You forgot to print something.
Federico klez Culloca
  • 26,308
  • 17
  • 56
  • 95
NurD
  • 1
  • 2
  • 1
    As an aside, `sumRow` prints nothing by itself. You have to [print the array it returns](https://stackoverflow.com/questions/409784/whats-the-simplest-way-to-print-a-java-array). – Federico klez Culloca Aug 08 '22 at 09:49
  • `sumRow(array)`?! – Jens Aug 08 '22 at 09:49
  • a function is like a machine where you can put something in and get something out. you have to give the arrray into it and the parameter row is not needed – Andreas Radauer Aug 08 '22 at 09:49
  • Please read the help pages on how to format your question correctly. You're supposed to print 3 different sums, one on each line, but because of your lack of formatting your question appears to show a number on just one line. – k314159 Aug 08 '22 at 09:50
  • 1
    posted "option" should not compile - missing arguments to `sumRow` call – user16320675 Aug 08 '22 at 10:00
  • You need to `System.out.println(sumrow(array, row));` for all rows. – k314159 Aug 08 '22 at 10:28
  • To get proper answer please first describe what specific result you expect. Also note that `print` lets us *display* value, like on console. The `return` lets us *give* value back to other part of a program which asked for it. BTW `sumRow` is declared to require array and row number but you are not providing it in your example. – Pshemo Aug 08 '22 at 10:32

1 Answers1

0
public static int sumRow(int[][] array, int row)

this requires a matrix int[][] array and an integer int row as inputs.
then it outputs an integer static int as output.
Therefore when you call the function you must write it like so:

sumrow(array, row);

And print it out like so:

System.out.println(sumrow(array, row));

As pointed out by @k314159.

To get the output of all three arrays inside the matrix you would have to:

System.out.println(sumrow(array, 0));
System.out.println(sumrow(array, 1));
System.out.println(sumrow(array, 2));

And this should output the sum of all three arrays in the matrix.

Also I would like to point out that int[][] is a matrix and not an array, so it would be more "correct" to declare it like so:

public class Sum extends ConsoleProgram {
    public void run() {
        int[][] matrix = {{32, 4, 14, 65, 23, 6},
                        {4, 2, 53, 31, 765, 34},
                        {64235, 23, 522, 124, 42}};
    }

    public static int sumRow(int[][] matrix, int row) {
        int sum = 0;
        for(int col = 0; col < matrix[row].length; col++) {
             sum += matrix[row][col];
        }
        return sum;
    }
}
The Blind Hawk
  • 1,199
  • 8
  • 24
  • It would also be good to use a loop instead of hard-coding the rows as `0`, `1` and `2`, but perhaps better to leave that as a learning exercise for the OP. – k314159 Aug 08 '22 at 10:58