I have this Die class, which I am working on for a game called Craps. My old method works, but I wanted to try to use dice with a higher number than 6. I can get the program to accept my user input, but how can I get it to print out the value of 'value'? My current println method isn't working, as the compiler just skips it. Does anyone have any suggestions?

import java.util.Scanner;
public class Die
    private int value;
    private int faces;

    public static void main(String[] args)
    System.out.print("Enter a value for n:" );
    Scanner kboard = new Scanner (System.in);
    int n = kboard.nextInt();

    public void roll()
        value = (1 + (int) (6 * Math.random()));

    public void roll(int n)
        value = (1 + (int) (n * Math.random()));

    public int getNumDots()
        return value;

Edited by Dani: Code formatting

5 Years
Discussion Span
Last Post by DavidKroukamp

I see a println(value) on line 20 in your code. Is that the code that does not work? Where do you call the method: getNumDots()? You need to call that method to have it be executed.

the compiler just skips it.

Please explain what happens when you compile the program. How does the compiler "skip" lines of code?


Sorry, that was unclear.. it compiles correctly, but when the program is ran the println statement is just skipped. To call getNumDots(), would I use value.getNumDots(); in public void roll(int n)?

If I try value.getNumDots(), it says that "int cannot be dereferenced"

Edited by polarbear125


As NormR1 said you don't appear to call the getNumDots() method. Also why are you returning value in getNumDots() when you haven't done anything with value in that method. Other than that your methods look ok you just need to call them at some point.


I tried calling value.getNumDots(), but I get the "int cannot be dereferenced" error. I am returning value because I want it to be private in that class, it's part of a project with five other classes.


To call a method in a class, you need an instance of the class: aRefToClass.aMethodInClass()
In your code:


value is not an instance of the class containing the getNumDots() method. value is a primitive and does not have any methods that you can call.
Create an instance of the class and use that to call the method.

Where do you set the value of the variable: value?


.>.< I'm terrible with things like that.. would something like NumDots integer = new NumDots(); be in the main method,and then I would call it some how.. but I don't know how to link it with value

Edited by polarbear125


Is there a class named NumDots?
The class with the getNumDots() method is Die. Create an instance of that class so you can call its methods.

You should read in the tutorial about classes and methods:
Click Here


adding this to your main method should do the trick:

        Die die = new Die();//create new instance of Die 
        die.roll();//calls instance method roll() in Die to give value a number
        System.out.println(die.getNumDots());//print out the instance value of die
This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.