0

I'm working on a program for my classes lab and I seem to be using nextInt wrong and I was wondering if someone could help. I couldn't find a post that had an answer. The program is designed to experiment with loops in a rolling dice simulation. It it supposed to roll two dice, determine the result, and then make a tally if it's snake eyes or doubles or whatever it may be.

So I get the following error:

DiceSimulation.java:33: error: cannot find symbol
     die1Value = nextInt();
                 ^
symbol:   method nextInt()
location: class DiceSimulation

DiceSimulation.java:34: error: cannot find symbol
     die2Value = nextInt();
                 ^
symbol:   method nextInt()
location: class DiceSimulation
2 errors

This is the code I have, I'm not entirely sure how I've used nextInt wrong.

   while (count < NUMBER)
   {
     die1Value = nextInt();
     die2Value = nextInt();
     if (die1Value == die2Value)
     {
        if (die1Value == 1)
        {   
           snakeEyes += 1;
        }
        else if (die1Value == 2)
        {   
           twos += 1;
        }   
        else if (die1Value == 3)
        {   
           threes += 1;
        }   
        else if (die1Value == 4)
        {   
           fours += 1;
        }   
        else if (die1Value == 5)
        {   
           fives += 1;
        }   
        else if (die1Value == 6)
        {   
           sixes += 1;   
        }
      }
      count += 1;
   }
3
  • Is it nextInt() from Scanner class? Commented Feb 10, 2014 at 1:30
  • you don't have a method nextInt() in your code, that's what the error is telling you Commented Feb 10, 2014 at 1:30
  • 1
    random.nextInt() - you need to specify the appropriate [Random] object instance .. Commented Feb 10, 2014 at 1:30

1 Answer 1

4

Random#nextInt() is neither a static method, nor built-in - you have to have an instance of the Random class to use it.

Here's an example:

Random dice = new Random();
int die1Value = dice.nextInt(6) + 1;
int die2Value = dice.nextInt(6) + 1;

The addition of 1 there is to offset the fact that a random value with a range bound generates values between [0, n).

Sign up to request clarification or add additional context in comments.

5 Comments

Asker wants to roll two dice, not ask for input.
So it's not a Scanner, but a Random. Two seconds.
Still doesn't work; you have to specify the range of the int to return.
Indeed, nextInt() is quite ambiguous on its own. I made the same mistake reading the OP's question
@Saposhiente: As far as it "working", the signature is correct. As far as it working within the bounds of the program, perhaps you have a point - using nextInt(n) would be correct.

Your Answer

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.