I am trying to make a bot that selects a random joke from an array list, but I seem to get an error that says:
Variable expected
My code so far is:
package com.delta.objects;
import java.util.ArrayList;
/**
* Created by WILLIAM on 8/3/2015.
*/
public class JokeBot extends Bot {
public ArrayList<Joke> jokesIKnow = null;
public JokeBot(ArrayList<Joke> jokesIKnow) {
this.jokesIKnow = jokesIKnow;
}
public void tellJoke(){
Double randomNumDouble = new Double(Math.random() = jokesIKnow.size());
int randomNum = randomNumDouble.intValue();
}
protected void sayJoke(Joke aJoke){
talk(aJoke.getJokeSetup());
talk(aJoke.getJokePunchline());
}
}
the error comes up for:
Double randomNumDouble = new Double(Math.random() = jokesIKnow.size());
-
1What do you expect that line of code to do?Jeroen Vannevel– Jeroen Vannevel2015年08月03日 22:13:42 +00:00Commented Aug 3, 2015 at 22:13
1 Answer 1
Double randomNumDouble = new Double(Math.random() = jokesIKnow.size());
That's some very invalid syntax. You can't assign the return value of a method (in this case jokesIKnow.size() is a method which returns something) to anything except a variable. For example, this is legal:
int numberOfJokes = jokesIKnow.size();
Here you are trying to assign it to another method. Perhaps you mean to write Math.random(jokesIKnow.size()) which passes the variable into the random generator.
answered Aug 3, 2015 at 22:14
Kon
10.8k6 gold badges46 silver badges60 bronze badges
Sign up to request clarification or add additional context in comments.
1 Comment
Neuromeda
Your answer indirectly answered my question. I meant to put * instead of =, so now my code is:
Double randomNumDouble = new Double(Math.random() * jokesIKnow.size()); I can understand the downvote however. That is a messy way to do it when your way is much more simple.lang-java