Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Variable expected error

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());
like image 298
Neuromeda Avatar asked May 26 '26 13:05

Neuromeda


1 Answers

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.

like image 60
Kon Avatar answered May 30 '26 07:05

Kon



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!