Forums Register Login

cant find API method problems

+Pie Number of slices to send: Send
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
+Pie Number of slices to send: Send
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).
+Pie Number of slices to send: Send
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
+Pie Number of slices to send: Send

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).
+Pie Number of slices to send: Send
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 :)
I miss the old days when I would think up a sinister scheme for world domination and you would show a little emotional support. So just look at this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com


reply
reply
This thread has been viewed 1502 times.
Similar Threads
constructor problems
problem with logical condition
cant find symbol problems
cant find variables problems
static problems
More...

All times above are in ranch (not your local) time.
The current ranch time is
Apr 16, 2024 08:25:59.