Hi All,

I am needing some assistance on how I can grab the values for the number of die that come up as 1, 2, 3, 4, 5 or 6. I have the count variables holding the count for each one that fits the "if statement". But I can't use the count variables(onesCnt, twosCnt and etc)within the public static void main part of the code as it is saying "non static variable onesCnt cannot be referenced from a static context". Can someone tell me how I can grab the values so I can print out the output on the screen for the count variables? I tried to create a get method to use the return method to grab the count variables but that didn't work either :(

public class Die 
{

public static final int MAXROLLS = 100;
public int[] dieRollCnt;
private int onesCnt;
public int twosCnt;
public int threesCnt;
public int foursCnt;
public int fivesCnt;
public int sixesCnt;

public static void main(String[] args)
{

   //System.out.println("This concludes the example" + onesCnt);
}



public void die(int[] dot)
{
   System.out.println("We will roll the die 100 times and store the input for the variables");
   int dieRollCount[] = new int[MAXROLLS];
   int oneCount = 0;
   int twoCount = 0;
   int threeCount = 0;
   int fourCount = 0;
   int fiveCount = 0;
   int sixCount = 0;
   for(int i=0; i < dieRollCount.length; i++)
   {
   //System.out.println("Random Number ["+ (i+1) + "] : " + (int)(Math.random()*6));
   dieRollCount[i]  =  (int) (Math.random()*6) + 1;
   //System.out.println("Die Roll [" + (i+1) + "] + dieRollCount[i]");
   System.out.println("Die Roll Number ["+ (i+1) + " + is ] = " + dieRollCount[i]);
   if(dieRollCount[i] == 1 )
   {
       onesCnt += oneCount++;


   }
   else if(dieRollCount[i] == 2)
   {
       twosCnt += twoCount++;
   }
   else if(dieRollCount[i] == 3)
   {
       threesCnt += threeCount++;
   }
   else if(dieRollCount[i] == 4)
   {
       foursCnt += fourCount++;
   }
   else if(dieRollCount[i] == 5)
   {
       fivesCnt += fiveCount++;
   }
   else if(dieRollCount[i] == 6)
   {
       sixesCnt += sixCount++;
   }
   else
   System.out.println("Please contact the developer as the code is not"
   + "functioning correctly");

   } 
}

}

Dani AI

Generated

— you hit two common issues: trying to read instance fields from static main, and a buggy increment pattern inside the loop. was correct that you either make the counters/methods static or you instantiate the class. Prefer instantiating and keeping counters private; it keeps the design cleaner and avoids accidental shared state.

A simple, clearer pattern is to keep one counter array and increment the right slot. That removes duplicate local counters and the confusing onesCnt += oneCount++ (which adds the old oneCount value and then increments it). Use a single source of truth, expose safe getters, and print from main after rolling:

public class Die {
  private final int[] counts = new int[6];

  public void roll(int times) {
    java.util.Random rnd = new java.util.Random();
    for (int i = 0; i < times; i++) {
      counts[rnd.nextInt(6)]++;
    }
  }

  public int getCount(int face) { return counts[face - 1]; }
  public int[] getCounts() { return counts.clone(); }

  public static void main(String[] args) {
    Die d = new Die();
    d.roll(100);
    for (int f = 1; f <= 6; f++) {
      System.out.println(f + " => " + d.getCount(f));
    }
  }
}

Troubleshooting notes:

  • Don’t keep both onesCnt and a separate oneCount — pick one and ++ it. onesCnt++ increments directly and is the usual choice.
  • If you need concurrency or better randomness, use ThreadLocalRandom.current() (in Java 7+) instead of new Random().
  • If you made the fields static (what you tried in main), remember they are shared across all instances and harder to test.

This approach follows encapsulation, fixes the increment logic, and cleanly separates rolling from reporting.

Recommended Answers

All 3 Replies

Since you're not instantiating any objects of class Die, anything you want to use must be declared static. That includes all the variables that are defined directly within the class as well as the die() method.

That said, you don't have code showing how you expect to use the Die class (namely the body of main), so I could be way off base.

Yes that is my question. How can I grab the values that I am incrementing in the if else loops so that I can print out the values? Do I need to restructure everything?

I have fixed the code I just took out the variables I had in the class Die. And just used the variables in the static void main portion to get the value from the counts of the variables.

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.