0

I am a bit confused on how I would take the randomly generated number in a range from my program, store that into an array, and read and print out from the array how many times that the number was generated.

For the random import I am using java.util.concurrent.ThreadLocalRandom;

public static void main(String[] args) { 
 char quitOption = 'q'; 
 char continueOption = 'c';
 char input;
 int[] myArray;
 Scanner console = new Scanner(System.in);
 do {
 int roll = ThreadLocalRandom.current().nextInt(1, 6);
 System.out.println("Roll is " + roll);
 System.out.println("Enter c to continue or enter q to quit ");
 input = console.nextLine().charAt(0);
 if (input == continueOption || input == 'C') {
 roll = ThreadLocalRandom.current().nextInt(1, 6);
 System.out.println("Roll is " + roll);
 System.out.println("Enter c to continue or enter q to quit ");
 input = console.nextLine().charAt(0);
 } else if (input == quitOption || input == 'Q') {
 System.exit(0);
 }
 } while (continueOption == 'c' || continueOption == 'C');
}
MTCoster
6,1843 gold badges33 silver badges55 bronze badges
asked Dec 20, 2018 at 14:50
2
  • I'd use an ArrayList to store your numbers since the number of inputs is dynamic. To get the number of repeats, you can sort the list then keep checking if its neighbor is the same number Commented Dec 20, 2018 at 14:58
  • 1
    @user3170251 Actually, he should use a HashMap<int, int> and then each time he encounters a roll, he should up the value in the hashMap. Commented Dec 20, 2018 at 14:59

2 Answers 2

2

I would use a HashMap<Integer, Integer> lets call it rollMap.

Each time you roll you get int currentRoll = randomRoll().

If I were you, I would then say:

if(rollMap.containsKey(currentRoll)){
 rollMap.put(currentRoll, rollMap.get(currentRoll) + 1);
}else{
 rollMap.put(currentRoll, 1);
}

You can then get how many times each number was rolled by saying:

System.out.println(rollMap.get(<rollid>));
answered Dec 20, 2018 at 15:04
Sign up to request clarification or add additional context in comments.

Comments

1

You must figure out how to overcome two problems:

  • Figure out how many rolls there will be, as Array's are fixed sized

  • Count how many time a number is rolled

You could use a List, and then use built in methods such as Collections.frequency, or if you are confined to an Array, check to make sure that adding another number will not be out of bounds, (And if it will be then copying it to a new Array) and then iterating over the Array and counting how many times each number occurs.

However, we know the range of numbers that will occur. So why not initialize an Array with six elements, and let 0 be 1, 1 be 2, and so on. Then every time that number is rolled, we increment the index of the respective number. So something like:

int roll = ThreadLocalRandom.current().nextInt(1, 6);
arr[roll -1]++;

So if a two is rolled, we will add one to the 1th index:

[0, 1, 0, 0, 0, 0]

And so on. Then when you need to count the index its a simple loop:

for(int i = 0; i < arr.length; i++) {
 System.out.println(i + 1 + " occurs: " + arr[i] + " times");
}

Also you are over complicating your loop. It can be simplified to:

char input;
int[] myArray = new int[6];
Scanner console = new Scanner(System.in);
do {
 int roll = ThreadLocalRandom.current().nextInt(1, 6);
 System.out.println("Roll is " + roll);
 myArray[roll -1]++;
 System.out.println("Enter c to continue or enter q to quit ");
 input = console.nextLine().charAt(0);
} while (input == 'c' || input == 'C');
for(int i = 0; i < myArray.length; i++ ) {
 System.out.println(i + 1 + " occurs: " + myArray[i] + " times");
}

Sample run:

Roll is 4
Enter c to continue or enter q to quit 
c
Roll is 1
Enter c to continue or enter q to quit 
c
Roll is 3
Enter c to continue or enter q to quit 
c
Roll is 3
Enter c to continue or enter q to quit 
c
Roll is 1
Enter c to continue or enter q to quit 
c
Roll is 1
Enter c to continue or enter q to quit 
q
1 occurs: 3 times
2 occurs: 0 times
3 occurs: 2 times
4 occurs: 1 times
5 occurs: 0 times
6 occurs: 0 times
answered Dec 20, 2018 at 15:01

1 Comment

This is actually was simpler than mine, I overthought it. HAHA

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.