i am very new to Java (1st semester). The assignment is to return the location of the largest element in a 2-diamensional array by using this method <public static int[] locatelargest(double[][] a).

I have worked on this program for almost a week and have eliminated all but the following two '.class expected' errors.

Please any information and/or help is appreciated.

import java.util.Scanner; // Scanner is in the java.util package
public class testTwoDiaArray {
/**Main Method*/
   public static void main(String[] args) {
      java.util.Scanner input = new java.util.Scanner(System.in);
      //have user enter the number of rows and columns to be processed
      System.out.println("Enter the number of rows and columns of the array");    
      int r = input.nextInt();
      int c = input.nextInt();

      System.out.println("rows: " + r  + "   cols: " + c);

      //get and store the number of rows and cols
      double[][] rowCol = new double[r][c];
      double[][] a = new double[r][c];
      System.out.println("Enter the array");
      for (int rows = 0; rows < rowCol.length; rows++) { 
         for (int cols = 0; cols < rowCol[rows].length; cols++) {
           rowCol[rows][cols] = input.nextDouble();
           int[] result = locateLargest(double[][] a);
           System.out.println("The location of the largest element is at (" + a[0] + ", " + a[1] + "0");

           }
      }
      }   
      public static int[] locateLargest(double[][] a) {
      double largest;
      largest = 0;
      for (int lrow = 0; lrow < a.length; lrow++) { 
         System.out.println("a length is " + a.length);                                  
         for (int lcol = 0; lcol < a[lrow].length; lcol++) {
           if (a[lrow][lcol] > largest) largest = a[lrow][lcol];

         }
      }
       return result; 
  }
}

you have a few errors here:
the error you talk about now - once you've declared a variable, you can refer to it using the reference alone.
replace

       int[] result = locateLargest(double[][] a);

by

       int[] result = locateLargest(a);

the way you have it is invalid syntax.

next, you'll get an error in your method, since you have:
return result;
but you don't have such a variable in the method itself.

I wish I could help you here completely, but I don't understand the logic of returning an array of integers here, I thought it was supposed to be the largest element ?

anyway, you can solve this by replacing:
return result;
to
return new int[1];

it won't do what you want it to do, but I can't explain how to do that, since I don't really understand what you try to do there.

next issue:

import java.util.SCANNER;

there is no class called SCANNER
Java is case sensitive, make sure you remember that.
replace the import statemen with:

import java.util.Scanner;

actually, you can drop the import statement, since you use the complete path for your variable.
make sure you replace it there as well:

  java.util.Scanner input = new java.util.SCANNER(System.in);

should be

  java.util.Scanner input = new java.util.Scanner(System.in);

it won't be finished yet, but it'll run and help you get on the way

thank you very much for your help. I missed several classes due to illness and am sure that I am not understanding it the way I should have. Sorry if I confused you.

I got the program compiled and running now am trying to return the location of the largest element by giving the row and column it is located in. Hopefully I will be able to figure it out soon, but your help was extremely appreciated.

well ... what you are trying to do is a bit ... wrong. you should return two coordinates, not a two dimensional array.
two ways I can think of:
1. create a new class 'Coordinate', which takes two values: the index of the first, and the index of the second array
2. which is probably better for you, if you don't know yet how to work with your own classes:
return a String, containing the coordinates, something like
"[coordinateX]-[coordinateY]" (example: "3-4")
and in your main method, use the split method of String to get the different coordinates

I am still having trouble.
What I am trying to do is to locate the largest element
in a 2-diamensional array. The book problem is as follows:
and I quote:
Bold Text Here"Write the following method that returns the location of the largest element in a 2-diamensional array using

public static int[] locatelargest(double[][] a)

The return value is a one-diamensional array that contains 2 elements. They indicate the row and column indices of the largest element in the 2-dimensional array. Write a program that prompts the user to enter a two-dismensional array and displays the location of the largest element in the array.Bold Text Here

I made the suggested changes and was able to complile and run the program, but I cannot get the "method' to work. the 2-d field 'a' is populated with data prior to calling the 'method' but when it goes to the method there are no value in it that I can determine using the println command. The program is below. I am sure that I am doing something very stupid, but can't figure it out.

In this section I have the user enter the number of rows and columns to be processed then pass this info to 'rowCol' and 'a' defined as double[][].

import java.util.Scanner; 
public class twoDiaArray {
/**Main Method*/
   public static void main(String[] args) {
  java.util.Scanner input = new java.util.Scanner(System.in);

  System.out.println("Enter the number of rows and columns of the array");    
  int r = input.nextInt();
  int c = input.nextInt();

In this section rowCol and a is defined as double[][]. I tried to define 'a' many different ways, but the only way I could get it to work was below. This may be the problem, but I was able to pass the array to it further down and there is info in it just before I call the method. But there is not data in 'a' once I get to the 'method'.

  //get and store the number of rows and cols
  double[][] rowCol = new double[r][c];
  double[][] a = new double[r][c];

  System.out.println("Enter the array");
  for (int rows = 0; rows < rowCol.length; rows++) { 
    for (int cols = 0; cols < rowCol[rows].length; cols++) {
         rowCol[rows][cols] = input.nextDouble();
         a[rows][cols] = rowCol[rows][cols];

    System.out.println("entered array a: " + a[rows][cols]);

   int[] find = locateLargest(a);
     System.out.println("entered retn a: " + a[rows][cols]);
     }
  }
   System.out.println("The location of the largest element is at (" + a[0]  + ", " + a[1] + ")");
  }   

This section processes the method. There is no data in 'locateLargest(double[][] a)'.

  public static int[] locateLargest(double[][] a) {
  double largest = 0;
  for (int lrow = 0; lrow < a.length; lrow++) { 
     for (int lcol = 0; lcol < a[lrow].length; lcol++) {
      if (a[lrow][lcol] > largest){
           largest = a[lrow][lcol]; 
     }
  }
  }
           return new int[1];
 }
}

Once I do the return 'a' still have the original data that was suppose to be sent to 'method.

How do I get the method section to read what is in 'a'.

How do I return int[1] and read and process it when it is returned.

I really hope you can help. Any information or suggestion is truly appreciated. Thank you.

Sorry, I didn't refresh my screen from 2 hrs ago and didn't read your reply.

Unfortunately, we haven't learned either of these two methods- creating classes or spliting strings. Our teacher wanted us to write a 2-d program sending an array and returning an array.

How would I defind 'a' to call the method the book wanted us to use. I tried so many different ways to defind 'a' but the only way was 'double[][] a = new double[r][c];' and I know this is wrong, but nothing else would compile.

' public static int[] locatelargest(double[][] a) '

How would I return 'a' from the method. Your suggestion
"return int[1]" doesn't return anything to 'a'.

I know that I am asking some very stupid questions but I spent hours now working on this and can't figure it out.

Thank you for any help you can give

as I said, that was not a sollution towards a functional program, only one that could compile and run without throwing an exception.

in your locateLargest method, you keep a local variable 'largest'.
add two more, two ints, being row and col (or another name).

row and col are linked to the indices where you find the largest element, which you need to overwrite, each time you find a larger one.

then at the end of your method: return {row, col};

I added the code but still am having problems with the return. I have not idea what I am doing wrong. I've tried making changes to how I define and initalize the 'a', how I pass the information to 'a' and call the method, and how I return the data. Nothing has worked.

When using your code to return ""return {indxrow, indxcol}"" with my file names I get this error message:

twoDiaArray.java:58: error: illegal start of expression
                  return {indxrow, indexcol);
                         ^
twoDiaArray.java:58: error: not a statement
                  return {indxrow, indexcol);
                          ^

The problem could be with how I defined 'a'. I defined it as follows --

 double[][] a = new double[r][c];

I then pass the data to 'a' with this code --
"a[rows][cols] = rowCol[rows][cols]" -- befoe I call the method with the following code:

 System.out.println("Enter the array");
   for (int rows = 0; rows < rowCol.length; rows++) { 
    for (int cols = 0; cols < rowCol[rows].length; cols++) {
       rowCol[rows][cols] = input.nextDouble();

       a[rows][cols] = rowCol[rows][cols];
       int[] find = locateLargest(a);

         }
      }

My method code looks like this:

  public static int[] locateLargest(double[][] a) {
  double largest = 0;
  int indxrow;
  int indxcol;

  for (int lcol = 0; lcol < a[0].length; lcol++ ) {
     largest = a[0][lcol];
     indxcol = lcol;
  }   

  for (int lrow = 1;  lrow  <  a.length;  lrow++)  { 
      for (int lcol = 0;  lcol  <  a[lrow].length;  lcol++) 
           if (a[lrow][lcol] > largest) {  
              largest = a[lrow][lcol];
              //
           }
           indxrow = lrow;
  }
             //  return largest;
             //  return largest[indxrow][indxcol];     
             //return [indxrow][indxcol];
              return {indxrow, indexcol);
            // return result; 
   }     
 } 

I've tried all sorts of combinations for the return, for defining 'a', for calling the method. Nothing works.

Please help me one last (hopefully) time. I have really appreciated your help.

Have a very Happy Thanksgiving.

first of all, you should initialize them when you declare them. also, you should update the values each time you find a higher element. so ...

double largest = 0;
  int indxrow;
  int indxcol;

becomes:

double largest = 0;
  int indxrow = 0;
  int indxcol = 0;

this will fix the error you are having, since now those variables do point to a value (remember: local variables don't have default values, like instance variables do)

one line 17, you have this:

indxrow = lrow;

that is bad. it'll overwrite the value, whether or not it should point to this lrow value. only overwrite it, if you find a higher value.

so, this:

if (a[lrow][lcol] > largest) {  
              largest = a[lrow][lcol];
              //
           }
           indxrow = lrow;

should be something like:

if (a[lrow][lcol] > largest) {  
              largest = a[lrow][lcol];
              indxrow = lrow;
              indxcol = lcol;
           }

Thank for your help. i made those changes and am still getting the same 5error msg. I am sorry I must seem dumb, but I can't see what I am doing wrong. I've tried your suggestions and many other ways, but keep getting this.

I hope your are enjoying your Thanksgiving.

twoDiaArray.java:63: error: illegal start of expression
             return [indxrow, indexcol];
                    ^
twoDiaArray.java:63: error: not a statement
             return [indxrow, indexcol];
                     ^
twoDiaArray.java:63: error: ';' expected
             return [indxrow, indexcol];
                            ^
twoDiaArray.java:63: error: not a statement
             return [indxrow, indexcol];
                              ^
twoDiaArray.java:63: error: ';' expected                 s

The changes are as follows:

 public static int[] locateLargest(double[][] a) {
      double largest = 0;
      //int[] largest = {0, 0};
      int indxrow = 0;
      int indxcol = 0;

      for (int lcol = 0; lcol < a[0].length; lcol++ ) {
          largest = a[0][lcol];
          indxcol = lcol;  }   

     for (int lrow = 1;  lrow  <  a.length;  lrow++)  { 
       for (int lcol = 0;  lcol  <  a[lrow].length; lcol++) 
            if (a[lrow][lcol] > largest) {  
               largest = a[lrow][lcol];
               indxrow = lrow;
               indxcol = lcol;
            }
         }
           return {indxrow, indexcol];
    }     
 } 

that's because you have:
return [indxrow, indexcol]; somewhere (on line 63 of twoDiaArray.java)
which should be
return {indxrow, indexcol};
but you are asking questions about two different classes testTwoDiaArray and twoDiaArray. can you please stick to one single one ?
I'll explain why this is a bit difficult to follow:
1. it's hard to see what code you are talking about
2. in one class, you have indexcol, in the other, it's indxcol.
this is very confusing.

Thank you very much for your help. I finally got it compiled and running correctly. I could not have done it without you and am very appreciated of your help.

I hope you had a great Thanksgiving and I also hope you have a better Christmas

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.