What I am doing incorrectly when creating a method in a class?

100 views Asked by At

I am trying to create a program where the user enters a 2d array, and the program will find the highest value in the array, and output the value, and the associated row and column. I have created a method (called locatelargest) inside the class that goes through all the points in the 2d array, and sets aside the MaxValue. Then it returns the MaxValue. I get errors for ) and ] expected, illegal start of type, ; expected, and identifier expected. How can I get the method and program overall to work? Thank you for your time.

import java.util.Scanner;

public class Find {

public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print ("Enter the number of rows in the the array: ");
int numberOfRows = input.nextInt();
System.out.print ("Enter the number of rows in the the array: ");
int numberOfColumns = input.nextInt();

double[][] anarray = new double[numberOfRows][numberOfColumns];


System.out.println("Enter the array: ");
for (int i = 0; i < a.length; i++){
  for (int j = 0; j < a[i].length; j++){
    a[i][j] = input.nextDouble();
  }  
}
Location location = locatelargest(anarray);
System.out.println("The location of the largest element is "
  + location.maxValue + " at ("
  + location.row + ", " + location.column + ")");    


 }

 }

  class Location{
     public double maxValue;
     public int row;
     public int column;

        public static double[] locatelargest(double[r][c] array){

              for (int r = 0; r < array.length; r++){
                 for (int c = 0; c < array[r].length; c++){
                    if (b[row][column] > maxValue){
                       MaxValue = array[row][column];
                       row = r;
                       column = c;
                       return MaxValue;
                     }
                 }
              }
        }       
  }

Update: I have the program running correctly. Here is my code.

import java.util.Scanner;

public class Big {

public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print ("Enter the number of rows in the the array: ");
int numberOfRows = input.nextInt();
System.out.print ("Enter the number of rows in the the array: ");
int numberOfColumns = input.nextInt();

double[][] array = new double[numberOfRows][numberOfColumns];


System.out.println("Enter the array: ");
for (int i = 0; i < array.length; i++){
  for (int j = 0; j < array[i].length; j++){
    array[i][j] = input.nextDouble();
     }  
    }
Location location = new Location();

location.locatelargest(array);

System.out.println("The location of the largest element is "
  + location.maxValue + " at ("
  + location.row + ", " + location.column + ")");    


   }

   }

  class Location{
     public static double maxValue;
     public static int row;
     public static int column;

        public static double locatelargest(double[][] array){

              for (int r = 0; r < array.length; r++){
                 for (int c = 0; c < array[r].length; c++){
                    if (array[r][c] > maxValue){
                       maxValue = array[r][c];
                       row = r;
                       column = c;

                     }
                 }
              }
              return maxValue;
        }       
  }
1

There are 1 answers

0
ford prefect On BEST ANSWER

Some basic issues to get you started:

Your method signature should be public static double locatelargest(double[][] array) to start. unless you are trying to return an array of the row and column which would be an int[]

You should have

   if (b[r][c] > maxValue){
        MaxValue = array[r][c];

You need to move your return statement outside of your loop. You need to declare double maxValue (and don't capitalize the variable name)

You also need to call this as Location.locatelargest(array) and the method name would conventionally be locateLargest

TL;DR: Sotirios is right. You need to go back and read the manual.