• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Cannot find symbol

 
Joe Lemmer
Ranch Hand
Posts: 171
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Campbell Ritchie
Sheriff
Pie
Posts: 49367
62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
    Posts: 171
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Awesomeness!!!

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

    Cheers

    Joe :-)
     
    Jelle Klap
    Bartender
    Posts: 1952
    7
    Eclipse IDE Java
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Joe Lemmer wrote:
    ..which should keep me out of trouble in future.


    Auto-completion can do that too!
     
    Campbell Ritchie
    Sheriff
    Pie
    Posts: 49367
    62
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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
    Posts: 1952
    7
    Eclipse IDE Java
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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
    Pie
    Posts: 49367
    62
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    I think I agree with you, Jelle
     
    • Post Reply
    • Bookmark Topic Watch Topic
    • New Topic