0

I am trying to make a small example here. Lets say I have an array that looks like this:

userInput = input.nextInt();
int[] numberOfValues; 
numberOfValues = new int[10];
for (int x = userInput; x <= userInput+10; x++) {
    numberOfValues[x] = x+1;
}

So if the user input 1 then the array would be 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 but if the user inputs 2 then the array would be 2, 3, 4, 5, 6, 7, 8, 9, 10 ,11

How would I display how many of each number there is in the array if the number care different based on user input. I realize that there is only one of each number but my actual program is a lot longer.

shmosel
  • 49,289
  • 6
  • 73
  • 138
Caleb
  • 572
  • 1
  • 7
  • 25
  • Use a `HashMap` as a counter. – SedJ601 Mar 06 '18 at 02:54
  • 1
    What does your question have to do with your example code, if your example code isn't like your real code? Also, what have you tried in terms of counting each number? – Elliott Frisch Mar 06 '18 at 02:54
  • Use `numberOfValues.length` instead of `userInput+10` – Scary Wombat Mar 06 '18 at 02:55
  • Your example is going to give you an ArrayIndexOutOfBoundsException if the input is anything other than 0. – Dawood ibn Kareem Mar 06 '18 at 02:55
  • it is except that my actual code rolls dice. The dice can have different amount of sides and the user can also choose how many dice there are. This means I cant just make a counter for a possible value such as two and increment it since it is possible the user uses four dice which means two will never appear. Also the dice are rolled 100000 times. – Caleb Mar 06 '18 at 02:57
  • How do I use HashMap Sedrick I have never heard of it. – Caleb Mar 06 '18 at 02:58
  • *I have never heard of it* **but** have you tried searching for it? – Scary Wombat Mar 06 '18 at 03:00
  • I just looked it up now. I can't use it because my teacher has not taught it. – Caleb Mar 06 '18 at 03:01

0 Answers0