Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cannot find symbol Java error?

The code works when I used java.util.Arrays.sort(numbers); Am I doing something wrong? This seems weird to me.

import java.util.Arrays.*;

class Test {
   public static void main(String[] args) {
    double[] numbers = {6.0, 4.4, 1.9, 2.9, 3.4, 3.5};
    char[] chars = {'a', 'A', '4', 'F', 'D', 'P'};

    sort(numbers);

    System.out.println(binarySearch(numbers, 3));

   }
}

(Error displayed in terminal)

Test.java:8: error: cannot find symbol
    sort(numbers);
    ^
symbol:   method sort(double[])
location: class Test
 Test.java:10: error: cannot find symbol
    System.out.println(binarySearch(numbers, 3));
                       ^
 symbol:   method binarySearch(double[],int)
 location: class Test
  2 errors
like image 503
JavaNoob Avatar asked May 24 '13 19:05

JavaNoob


People also ask

What to do if Cannot find symbol in Java?

Misspelled method name Misspelling an existing method, or any valid identifier, causes a cannot find symbol error. Java identifiers are case-sensitive, so any variation of an existing variable, method, class, interface, or package name will result in this error, as demonstrated in Fig.

What does error Cannot find symbol mean?

The “cannot find symbol” error comes up mainly when we try to use a variable that's not defined or declared in our program. When our code compiles, the compiler needs to verify all the identifiers we have. The error “cannot find symbol” means we're referring to something that the compiler doesn't know about.

What does an unknown symbol error usually indicate in Java?

It means that either there is a problem in your Java source code, or there is a problem in the way that you are compiling it.

What is the meaning of symbol in Java?

Created right after the release of the language, the Java logo depicted a blue coffee cup with red steam above it. The symbol was a tribute to the Java developers, who drank a lot of coffee while working on the language. As it was mostly coffee from Java coffee beans, the name and the symbol were chosen fast enough.


1 Answers

It's a static method of the class Arrays.

You should invoke it like this:

Arrays.sort(someArray);

Note you still have to import the Arrays class like this:

import java.util.Arrays;

Or as others have mentioned, if you do a static import you can omit the class name.

I would argue that Arrays.sort() is better for readability.

like image 56
jahroy Avatar answered Oct 25 '22 02:10

jahroy