r/javahelp • • Mar 31 '19

java.lang.OutOfMemoryError

I made a dice project for my AP Comp class where it would use the console to display dice constructed from an array of strings. Later I modified it so that it would display which numbers where rolled how many times. I was recently messing around with it and I changed the constants so it would generate ten random numbers per roll and complete one billion rolls. It ran the code up until it was supposed to parse the array and display how many times each number was rolled. Underneath is the code I have written:

import java.util.*; public class DiceNineSide { private static final int DICENUM = 10; private static final Scanner in = new Scanner(System.in); private static final int LIMIT = 1000000000; public static void main(String[] args) { int count = 0; int[] averageInt = new int[LIMIT]; String play = "yes"; String[][] setOfDice = constructDice(); checkDiceNum(); //play.equalsIgnoreCase("yes") while (count < LIMIT){ //printDice(constructDice()); String[][] diceSet = new String[DICENUM][DICENUM]; int[] storeInt = new int[DICENUM]; int sum = 0; rollDice(diceSet, setOfDice, storeInt); //printDice(diceSet); for(int i = 0; i < storeInt.length; i++){ sum += storeInt[i]; } averageInt[count] = sum; count++; //play = in.next(); //line(); } numberCount(averageInt); countNum(count); } private static String[][] constructDice(){ String topBot = " --------- "; String E = "| |"; String M1 = "| * |"; String M2 = "| * * |"; String L1 = "| * |"; String R1 = "| * |"; String M3 = "| * * * |"; String[] One = {topBot, E, M1, E, topBot}; String[] Two = {topBot, E, M2, E, topBot}; String[] Three = {topBot, L1, M1, R1, topBot}; String[] Four = {topBot, M2, E, M2, topBot}; String[] Five = {topBot, M2, M1, M2, topBot}; String[] Six = {topBot, M2, M2, M2, topBot}; return new String[][]{One, Two, Three, Four, Five, Six}; } private static void printDice(String[][] allDice){ for(int dice = 0; dice < allDice[0].length; dice++){ // for (int side = 0; side < allDice.length; side++) // System.out.print(allDice[side][dice] + "\t"); for (String[] anAllDice : allDice) System.out.print(anAllDice[dice] + "\t"); System.out.println(); } } private static void line(){ for(int i = 0; i < 14 * DICENUM; i++) System.out.print("-"); System.out.println(); } private static int[] rollDice(String[][] diceSet, String[][] setOfDice, int[] storeInt){ for(int i = 1; i <= DICENUM; i++){ int roll = (int) Math.round(Math.random() * (constructDice().length - 1)); diceSet[i - 1] = setOfDice[roll]; storeInt[i - 1] = roll + 1; } return storeInt; } private static void checkDiceNum(){ if(DICENUM <= 0){ System.exit(-2); } } private static void countNum(int count){ if(count == 1) System.out.println("You rolled once."); else System.out.println("You have rolled " + count + " times."); } private static void numberCount(int[] countDice){ int[] count = new int[LIMIT]; for(int i = 0; i < countDice.length; i++){ count[countDice[i] - 1]++; } for(int i = DICENUM - 1; i < (DICENUM * constructDice().length); i++){ if(count[i] != 1){ System.out.println("You rolled a " + (i + 1) + " - " + count[i] + " times."); }else{ System.out.println("You rolled a " + (i + 1) + " - 1 time."); } } } }

0 Upvotes

1 comment sorted by

9

u/chickenmeister Extreme Brewer Mar 31 '19
new int[LIMIT]

If LIMIT is 1 billion, then creating an int array of that size is going to require about 4 gigabytes.

Your code is not formatted, so it's hard to read; but based on your description, I don't think you would need to create an array this large for what you're trying to do. If you want to keep track of how many times each number was rolled, then you would only need an array whose length is equal to the number of possible outcomes of each roll. For example, if you're rolling a single six-sided die, then there are only six possibilities. Therefore, you would have an array of size 6. Each value in the array represents how many times that number was rolled. For example, when a 1 is rolled, you should increment the value at index 0.