I am having some difficulties getting this code to work properly. I cannot figure out how to correctly return all of my methods because I have the println listed inside the methods. So when I run my program, I get the println's , plus the return value. But I don't know how to fix this. Also on my getHighestInRow and getLowestInRow, I am not getting the correct output. I am getting the Highest and Lowest only from the first row instead of each of the rows. Any help would be greatly appreciated. Thanks!

public class Array2DOperations
{
   public static void main(String[] args)
   {
      int [][] numbers = { {100, 75, 90, 83, 85, 98},
                           {100, 100, 100, 100, 100, 100} };

      for (int row = 0; row < numbers.length; row++)
      {
        for (int col = 0; col < numbers[row].length; col++)
          System.out.print(numbers[row][col] + " ");
        System.out.println();
      }

      System.out.println(" ");

      // Display the sum of all the values in the array.
      System.out.println("The sum of the values " +
                         "is " + getTotal(numbers));

      System.out.println(" ");

      // Display the average of all the values in the array.
      System.out.println("The average of the values " +
                         "is " + getAverage(numbers));

      System.out.println(" ");

      // Display the total of the rows.
      System.out.println(getRowTotal(numbers));

      System.out.println(" ");

      // Display the total of the columns.
      System.out.println(getColumnTotal(numbers));

      System.out.println(" ");

      // Display the highest value in the row.
      System.out.println(getHighestInRow(numbers));

      System.out.println(" ");

      // Display the lowest value in the row.
      System.out.println(getLowestInRow(numbers));

   }

      /**
      The getTotal method accepts a 2D array 
      as its argument and returns the total of 
      all the values in the array.
      @param array The array to sum.
      @return The sum of the array elements.
   */

   public static int getTotal(int[][] array)
   {
      int total = 0;   // Accumulator
      
      // Sum the values in the array
      for (int row = 0; row < array.length; row++)
      {
         for (int col = 0; col < array[row].length; col++)
         total += array[row][col];
      }
      return total;
   }

   /**
      The getAverage method accepts a 2D array 
      as its argument and returns the average of 
      all the values in the array.
      @param array The array to average.
      @return The average of the array elements.
   */

   public static double getAverage(int[][] array)
   {
      double total = 0.0;   // Accumulator
      
      // Sum the values in the array
      for (int row = 0; row < array.length; row++)
      {
         for (int col = 0; col < array[row].length; col++)
         total += array[row][col];
         //int size = array[row][col].length;
      }
      double average = total / 12;

      return average;
   }

   /**
      The getRowTotal method accepts a 2D array 
      as its first argument and an integer as its
      second argument. The second argument is a 
      subscript of a row in the array. The method
      returns the total of the values in a specified row.
      @param array The row in the array to total.
      @return The total of the values in a specified row.
   */

   public static int getRowTotal(int[][] array)
   {
      int rowTotal=0;

      // Sum the values in the rows of the array
      for (int row = 0; row < array.length; row++)
      {
         rowTotal=0;
         // Sum a row
         for (int col = 0; col < array[row].length; col++)
            rowTotal += array[row][col];

         //Display the row's total.
         System.out.println("Total of row " + (row) + " is: " + rowTotal);
      }
      return rowTotal;
   }

   /**
      The getColumnTotal method accepts a 2D array 
      as its first argument and an integer as its
      second argument. The second arguement is a 
      subscript of a column in the array. The method
      returns the total of the values in a specified column.
      @param array The array to average.
      @return The total of the values in a specified column.
   */

   public static int getColumnTotal(int[][] array)
   {
      int colTotal=0;

      // Sum the values in the rows of the array
      for (int col = 0; col < array[0].length; col++)
      {
         colTotal=0;
         // Sum a column
         for (int row = 0; row < array.length; row++)
            colTotal += array[row][col];

         //Display the row's total.
         System.out.println("Total of column " + (col) + " is: " + colTotal);
      }
      return colTotal;
   }

   /**
      The getHighestInRow method accepts a 2D array 
      as its first argument and an integer as its
      second argument. The second arguement is a 
      subscript of a row in the array. The method
      returns the highest value in a specified row
      of the array.
      @param array The array to average.
      @return The highest value in a specified row
      of the array.
   */

   public static int getHighestInRow(int[][] array)
   {
    int highest = array[0][0];
    for (int row = 0; row < array.length; row++)
    {
      for (int col = 0; col < array[row].length; col++)
        {
         if (array[row][col] >= highest)
               highest = array[row][col];
        }
        //Display the row's total.
        System.out.println("Highest in Row " + (row) + " is: " + highest);
    }
    return highest;

         //Display the row's total.
         //System.out.println("Total of row: " + (row) + " is " + rowTotal);
   }

   /**
      The getLowestInRow method accepts a 2D array 
      as its first argument and an integer as its
      second argument. The second argument is a 
      subscript of a row in the array. The method
      returns the lowest value in a specified row
      of the array.
      @param array The array to average.
      @return The lowest value in a specified row
      of the array.
   */

   public static int getLowestInRow(int[][] array)
   {
    int lowest = array[0][0];
    for (int row = 0; row < array.length; row++)
    {
      for (int col = 0; col < array[row].length; col++)
        {
         if (array[row][col] <= lowest)
               lowest = array[row][col];
        }
        //Display the row's total.
        System.out.println("Lowest in Row " + (row) + " is: " + lowest);
    }
    return lowest;
   }
}

You should change your getHighestInRow method to take two parameters: The row number that you want to get the highest value of, and the array itself. Then in your main method you can use a for loop to loop over each row of the array, getting the highest of each row. You could do a similar thing for the lowest method. That would also enabled you to get rid of the print statements in the methods and relegate those print statements to your main method.

package pkg2doperations;

import java.util.Scanner;


 // @author Alawn George

public class Pkg2doperations 
{
    public static void main(String[] args) 
    {
       int[][] numbers = { {1, 2, 3}, {4, 5, 6}, {7, 8, 9} };

        int row = 0;
        int col = 0;

       Scanner keyboard = new Scanner(System.in);
       System.out.print("Pick a row number from 0-2:  ");
       row = keyboard.nextInt();
       int rowtotal =getRowTotal(numbers, row);


       System.out.print("Pick a column number from 0-2:  ");
       col = keyboard.nextInt();
        int coltotal =getColumnTotal(numbers, col);

       int total =getTotal(numbers);
       double average =getAverage(numbers);


       int rowhighest = getHighestInRow (numbers, row);
       int rowlowest = getLowestInRow (numbers,row);










 }// main
 public static int getTotal(int [][] numberlist)
 {
     int total= 0;

    for (int row = 0; row < numberlist.length; row++)
   {
      for (int col = 0; col < numberlist[row].length; col++)
         total += numberlist[row][col];
    } // for
System.out.println("The total is " + total); 
    return total;
 }  //main

  public static int getAverage(int [][] numberlist)
  {
    int average,total = 0;


    for (int row = 0; row < numberlist.length; row++)
   {
      for (int col = 0; col < numberlist[row].length; col++)
         total += numberlist[row][col];


    } // for
average = total/numberlist.length;
 System.out.println("The average is " + average);
    return average;

 }  //main


    public static int getRowTotal(int[][] array, int row)

   {
      int rowTotal=0;
      // Sum the values in the rows of the array

         rowTotal=0;
         // Sum a row
         for (int col = 0; col < array[row].length; col++)
            rowTotal += array[row][col];
         //Display the row's total.
         System.out.println("Total of row " + (row) + " is: " + rowTotal);

      return rowTotal;
   } 



    public static int getColumnTotal(int[][] array, int col)
   {
      int colTotal=0;
      // Sum the values in the rows of the array

         colTotal=0;

         // Sum a column
         for (int row = 0; row < array.length; row++)
            colTotal += array[row][col];
         //Display the row's total.
         System.out.println("Total of column " + (col) + " is: " + colTotal);

      return colTotal;
   }


     public static int getHighestInRow(int[][] array, int row)
   {

    int highest = array[0][0];
    for (int col = 0; col < array[row].length; col++)
    {
         if (array[row][col] >= highest)
               highest = array[row][col];
        }

        System.out.println("Highest in Row " + (row) + " is: " + highest);

    return highest;
   }

      public static int getLowestInRow(int[][] array, int row)
   {

      int lowest = array[row][0];  
      for (int col = 0; col < array[row].length; col++)
        {
         if (array[row][col] <= lowest)
               lowest = array[row][col];
        }

        System.out.println("Lowest in Row " + (row) + " is: " + lowest);


    return lowest;
   }

}

Why did you post that uncommented program on a two-year-old thread???

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.