This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes Cannot find symbol Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Cannot find symbol" Watch "Cannot find symbol" New topic
Author

Cannot find symbol

Joe Lemmer
Ranch Hand

Joined: Oct 24, 2008
Posts: 171
Hi there,

I'm really confused as to why my program won't compile when I'm sure I've done this a hundred times.

My class declares and initializes a string array with the other instance variables right at the top of the class like so:



Then in a method further down where I want to use a for loop to compare a string I have to all the elements in the blahDeeBlah array I start the for loop like this:



For some reason I get a 'cannot find symbol - symbol: method length() - location java.lang.String[] - and then it references the above for loop.

So, I looked at the documentation for arrays and thought perhaps that I should be using the getLength(insert array name here) method even though I'm sure I've used the other one in the past. But I still get a 'cannot find symbol - method getLength(java.lang.String[]) - location class myClass.

Can anyone shed some light on this for me?

Thanks guys

Regards

Joe Lemmer


OCPJP 85%
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38075
    
  22
It's not a String you are looking for length(), but an array. If you are feeling brave, have a look at the Java Language Specification, where you find that an array has the following members:
  • Ten of the eleven methods of Object, unchanged.
  • The clone() method overridden from Object.
  • The public final int field "length."
  • It is the last one you want, so spell it "length" rather than "length()" and it should work. No round brackets.
    Arrays don't have a length() method, only the length field. And yes, it is confusing.
    Joe Lemmer
    Ranch Hand

    Joined: Oct 24, 2008
    Posts: 171
    Awesomeness!!!

    Thanks yet again Campbell. Great info, which should keep me out of trouble in future.

    Cheers

    Joe :-)
    Jelle Klap
    Bartender

    Joined: Mar 10, 2008
    Posts: 1756
        
        7

    Joe Lemmer wrote:
    ..which should keep me out of trouble in future.


    Auto-completion can do that too!


    Build a man a fire, and he'll be warm for a day. Set a man on fire, and he'll be warm for the rest of his life.
    Campbell Ritchie
    Sheriff

    Joined: Oct 13, 2005
    Posts: 38075
        
      22
    You're welcome.

    Auto-completion is nice, but you usually need an IDE to use it, and real beginners ought to avoid IDEs until they are used to writing code by hand. At least you don't have to use a pencil and paper, like for the little bit of teaching I help with.
    Jelle Klap
    Bartender

    Joined: Mar 10, 2008
    Posts: 1756
        
        7

    Campbell Ritchie wrote:You're welcome.

    Auto-completion is nice, but you usually need an IDE to use it, and real beginners ought to avoid IDEs until they are used to writing code by hand. At least you don't have to use a pencil and paper, like for the little bit of teaching I help with.


    I agree that it's better for beginners to code sans IDE, but if anyone would try to take mine away from me...well let's just say they'd better bring a really big stick
    Campbell Ritchie
    Sheriff

    Joined: Oct 13, 2005
    Posts: 38075
        
      22
    I think I agree with you, Jelle
     
    I agree. Here's the link: http://aspose.com/file-tools
     
    subject: Cannot find symbol
     
    Similar Threads
    Create a Name class to manage different name changes of a person.
    Null pointer exception when trying to enter a blank for a response -- how to fix?
    Cannot find symbol using input boxes
    'class' or 'interface' expected
    Compiling Struts from CMD.