Cannot find symbol - when calling a method

6.9k views Asked by At

The purpose of this code is to find the maximum value in a user-inputted 2-dimensional array. The code makes sense but when I try and compile, it gives me to following error:

ERROR:

LocateLargestElement.java:41: error: cannot find symbol

int result = maxValue(userMatrix);
                            ^
symbol:   variable userMatrix

location: class LocateLargestElement

1 error

I tried to talk to my programming professor, but he was being real mature and would not help me. Basically, I am trying to make result the maxValue, but it says it cannot find userMatrix.

//Import utility

import java.util.Scanner;

//initialize program

public class LocateLargestElement
{
   public static void main (String [] args)
   {
      Scanner input = new Scanner(System.in);
      int userInt = 0;

      do
      {
         run(input);
         System.out.println("Do You Want To Continue? 1 for Yes, 2 for No: ");
         userInt = input.nextInt();
      }

      while (userInt == 1);
   }

   //METHOD run

   public static void run (Scanner input)
   {
      int result = maxValue(userMatrix); //<--- CANNOT FIND "userMatrix" THIS IS THE ERROR

      System.out.println("The largest value in the given Matrix is: " + result);
   }

   //METHOD ask user for number of rows

   public static int lengthRow (Scanner input)
   {
      System.out.println("Please enter the number of rows of the desired matrix: ");
      int lengthRow = input.nextInt();

      return lengthRow;
   }

   //METHOD ask user for number of columns

   public static int lengthColumn (Scanner input)
   {
      System.out.println("Please enter the number of columns of the desired matrix: ");
      int lengthColumn = input.nextInt();

      return lengthColumn;
   }

   //METHOD ask user for input of values

   public static int [][] userMatrix (Scanner input, int lengthRow, int lengthColumn)
   {
      int [][] userMatrix = new int [lengthRow][lengthColumn];

      System.out.println("Please enter the values for the matrix: ");
      for (int i = 0; i < lengthRow; i++)
      {
         for (int j = 0; j < lengthColumn; j++)
         {
            userMatrix[i][j] = input.nextInt();
         }
      }

      return userMatrix;
   }

   //METHOD find the largest element in the matrix

   public static int maxValue (int[][] userMatrix)
   {
      int maxValue = 0;
      for (int i = 0; i < userMatrix.length; i++) 
      {
         for (int j = 0; j < userMatrix[i].length; j++) 
         {
            if (userMatrix[i][j] > maxValue) 
            {
               maxValue = userMatrix[i][j];
            }
         }
      }

      return maxValue;
   }
}
3

There are 3 answers

1
JasonMArcher On BEST ANSWER

Your first problem is that you have a variable and a method named the same, this will confuse you on usage. It isn't a technical problem for the code, but it is a problem for anyone reading the code.

I think you probably intend something like this. You need to call the method and pass the arguments it requires. Since you have methods for obtaining the other values, you should gather those first. This will make your code function, but could be better structured.

public static void run (Scanner input)
{
    int row = lengthRow(input);
    int column = lengthColumn(input);
    int result = maxValue(userMatrix(input, row, column));

    System.out.println("The largest value in the given Matrix is: " + result);
}

I'll leave it as an exercise for the reader to make it even better.

0
ToYonos On

Try this :

int result = maxValue(
    userMatrix(
        input,
        lengthRow(input),
        lengthColumn(input)
    )
);

You have to call the methods you have defined.

2
user3437460 On

Where is your declaration for usermatrix? You didn't declare it in the scope where you got your errors.

   public static void run (int[][] userMatrix)
   {
      int result = maxValue(userMatrix); //<--- CANNOT FIND "userMatrix" THIS IS THE ERROR

      System.out.println("The largest value in the given Matrix is: " + result);
   }

or

public static void run (Scanner input)
   {
      int[][] userMatrix = new int[x][y];

      int result = maxValue(userMatrix); //<--- CANNOT FIND "userMatrix" THIS IS THE ERROR

      System.out.println("The largest value in the given Matrix is: " + result);
   }