0

i was trying to compare 2 variables in java but it gives me error and i cant figure it out.

I am reading matrix element then put it in temp then put that temp variable in an array. but it gives error when I try to put matrix element in temp and when I compare elements. error: array required, but float found. Anyone knows how to correct this ?

    public float[] toSortedArray()
  {
    float b[];
    float temp;
    int index=0;
    for(int i=1; i<=m; i++)
    {
      for(int j=1; j<=n; j++)
      {
        temp=a[m][n];
        b[index++]=temp;
      }
    }
      Arrays.sort(b);

        System.out.print("[");
      for(int z=0; z<(m*n)-1; z++)
      {
        System.out.print(b[z]+", ");
      } 
      System.out.print(b[(m*n)-1]+"]\n");
  }
4
  • What is a? It is never declared. Is it a field? Also please specify in which line the error occurs. Also use proper indentation. Finally, if you used an IDE such as Eclipse, you would quickly find most errors. Another one is that your method is missing a return statement. Commented Oct 21, 2014 at 16:43
  • m and n are also not declared in this snippet. I also think you want to use i and j inside the loop to access the "matrix". There is a lot wrong with this code. Commented Oct 21, 2014 at 16:44
  • this is just little part of my code i have declared m and n in the class as a private members. i tried also in eclipse but error is the same. it occurs here: temp=a[m][n]; b[index++]=temp; Commented Oct 21, 2014 at 16:51
  • Of course the error is the same. But with an IDE, you will get useful, instant feedback and it will automatically indent the code correctly etc. For the error, look into the answers you have received so far. Commented Oct 21, 2014 at 16:53

1 Answer 1

2

There are several things needed in this:

  1. Pass on the m, n parameter along with original 2D array like

    public float[] toSortedArray(float[][] a, int m, int n)
    
  2. define b array as

    float b[] = new float[m*n];
    
  3. In for loop (one within i and j var) (both loop should start with 0) use

    temp=a[i][j];
    

instead of

    temp=a[m][n];
  1. At the end return b.

    return b;
    
Sign up to request clarification or add additional context in comments.

5 Comments

Since m and n are probably supposed to be the dimensions of a, I don't think passing them is necessary. Just use a.length and a[i].length. However, the loop needs to be shifted by 1 because arrays in Java are 0-based.
if a is null then or just initialised like float a[][] = new float[5][];?
thank you it worked when i declared public float[] toSortedArray(float[][] a, int m, int n) but in main function toSortedArray() is empty and it gives another error because ublic float[] toSortedArray(float[][] a, int m, int n) and toSortedArray() are not same. Also i cant change main function. are there any other way to correct this ?
how are you going to get a[][] array and value of m and n? that would be the best place to call this method from like myclass.toSortedArray(a, m, n);
float[] arr = fm.toSortedArray(); i didnt do main its given in the example itself.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.