What dose it means

at Dice.rollDice(Dice.java:30)
java.lang.IllegalArgumentException: n must be positive
at java.util.Random.nextInt(Random.java:250)
at Dice.rollDice(Dice.java:30)
my code is :

import java.util.Random;

public class Dice
{
    // instance variables.
    private Random mRandom;
    private int mSides;

    /**
     * Constructor for objects of class Dice
     */
    public Dice(int sides)
    {
        sides = mSides;
        mRandom = new Random();
    }
   
    public int rollDice()
    { 
        return mRandom.nextInt(mSides);
    }
}

Recommended Answers

All 2 Replies

first of all this:
sides = mSides;
should be like this:
mSides = sides;

And the error you get is because the method:
mRandom.nextInt(mSides)
doesn't take as argument a negative or zero number; as the error message says it must be positive.

The reason you get the error is because when you do:
sides = mSides
the sides is the argument and you simply change its value. The mSides which is the class variable doesn't change and remains to 0 and you get error.

If you had:
mSides = sides
the argument sides will give value to the mSides of the class, and it will take the value you give it, so the call:
mRandom.nextInt(mSides)
will have what you give as argument

Thanks alot, I was realy blind.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.