3

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());
Bond
16.3k6 gold badges34 silver badges56 bronze badges
asked Aug 3, 2015 at 22:11
1
  • 1
    What do you expect that line of code to do? Commented Aug 3, 2015 at 22:13

1 Answer 1

6
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
Sign up to request clarification or add additional context in comments.

1 Comment

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.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.