• Post Reply Bookmark Topic Watch Topic
  • New Topic

cant find API method problems  RSS feed

 
Ben Hultin
Ranch Hand
Posts: 135
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am running into problems about API methods which I included the import java.util for it. Seems it is still not found, either that or I have set up the usage incorrectly.



.\Mouse.java:40: cannot find symbol
symbol : method length()
location: class java.util.ArrayList<Mouse>
int mouse2 = random.nextInt(mice.length());
^


.\Mouse.java:39: cannot find symbol
symbol : variable random
location: class Mouse
int mouse1 = random.nextInt(mice.length());




My Mouse java file:




I appreciate the help
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16060
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You have two different errors here.

As you can see in the API documentation, class ArrayList does not have a method called length(). The method you should use to find out how many elements an ArrayList has is called size().

The second error happens because the variable named random is not in scope at the place you're trying to use it. In fact, you haven't declared a variable called random anywhere. You're probably missing a line:

(where Random is from the java.util package).
 
Ben Hultin
Ranch Hand
Posts: 135
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks a lot for the help. I am not sure if it was obvious but I was meaning for random to have the Math.random() property.

I got the syntax you see from a tutorial online that was suppose to serve this very purpose of randomly selecting an element from the Arraylist.

here is another idea I had:



Hopefully I am getting somewhere
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16060
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Math is a class. Class Math does not have a member variable called random, so this line of code will not compile. See the API documentation for class Math.

There are two ways to make random numbers with the standard Java classes: either use the Random class, or use the random() method (not a variable, or "property") of class Math. You seem to be mixing those two things up in the above line of code. (nextInt() is a method of class Random).
 
David Newton
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Also, contrast this:with this:It's not a big deal, and obviously logically it's the same, but think about readability, and keeping things concise. Learning these little tricks now will be helpful in the long run. And everyone will think you're cool.

(I'm probably wrong about that last part, if my own life is any indication.)

Using a number to indicate gender is considered "magic"--there's nothing intrinsically male about the number 1 (well... I guess if we use our imagination maybe there is). In this case, male is 1, female is 0 (outraging feminists everywhere). It might make more sense, or at least be more clear, if there were constants:That, in turn, leads me to think that it would be even better to just encapsulate the gender selection into its own method:But this doesn't actually buy us very much, leading me to believe that the gender would be best expressed not as a boolean (isMale) but simply as a value, gender. Ideally this would be an enum, buying us all sorts of type safety and clarity. It would also allow the gender generation to be wrapped up in the Gender enum, perhaps as Gender.generateRandomGender().

Lastly, consider the following fragment:Ask yourself how much information those comments really add. Since you've named your variables appropriately, you're basically saying the same thing twice, but not really increasing the signal-to-noise ratio.

I know, more than you wanted or needed :)
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!