Adding 1 to an Array index each loop [duplicate]
This question already has an answer here:
How to prevent ArrayIndexOutOfBoundsException in Java?
23 answers
So, I'm working on a problem that has me adding two die rolls 'X' amount of times, then recording the frequency in an array, here is what I have
int counters = new int [11];
for (int i = 0; i <1001; i++){
//have to add +1 so I don't get 0
++counters[((die1.nextInt(6)+1)+(die2.nextInt(6)+1))];
}
System.out.print(counters);
And I'm getting this error:
java.lang.ArrayIndexOutOfBoundsException: 12
java arrays
marked as duplicate by EJoshuaS, GrumpyCrouton, ekad, Joakim Danielson, M-M Jan 18 at 17:50
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
|
show 6 more comments
This question already has an answer here:
How to prevent ArrayIndexOutOfBoundsException in Java?
23 answers
So, I'm working on a problem that has me adding two die rolls 'X' amount of times, then recording the frequency in an array, here is what I have
int counters = new int [11];
for (int i = 0; i <1001; i++){
//have to add +1 so I don't get 0
++counters[((die1.nextInt(6)+1)+(die2.nextInt(6)+1))];
}
System.out.print(counters);
And I'm getting this error:
java.lang.ArrayIndexOutOfBoundsException: 12
java arrays
marked as duplicate by EJoshuaS, GrumpyCrouton, ekad, Joakim Danielson, M-M Jan 18 at 17:50
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
@fantaghirocco what are you talking about "doesn't compile"?
– rustyx
Jan 18 at 17:23
It compiles fine for me...
– Stalemate Of Tuning
Jan 18 at 17:24
DoesnextInt(6)
return a value from 0-5, or 1-6? If anything my guess is you should be subtracting 1 from the result, not adding it.
– Stalemate Of Tuning
Jan 18 at 17:25
@fantaghirocco it compiles for me? Not sure what's going on there
– SoloTriesToLearn
Jan 18 at 17:25
1
two 0's is a 2, and two 5's is a 12
– Stalemate Of Tuning
Jan 18 at 17:37
|
show 6 more comments
This question already has an answer here:
How to prevent ArrayIndexOutOfBoundsException in Java?
23 answers
So, I'm working on a problem that has me adding two die rolls 'X' amount of times, then recording the frequency in an array, here is what I have
int counters = new int [11];
for (int i = 0; i <1001; i++){
//have to add +1 so I don't get 0
++counters[((die1.nextInt(6)+1)+(die2.nextInt(6)+1))];
}
System.out.print(counters);
And I'm getting this error:
java.lang.ArrayIndexOutOfBoundsException: 12
java arrays
This question already has an answer here:
How to prevent ArrayIndexOutOfBoundsException in Java?
23 answers
So, I'm working on a problem that has me adding two die rolls 'X' amount of times, then recording the frequency in an array, here is what I have
int counters = new int [11];
for (int i = 0; i <1001; i++){
//have to add +1 so I don't get 0
++counters[((die1.nextInt(6)+1)+(die2.nextInt(6)+1))];
}
System.out.print(counters);
And I'm getting this error:
java.lang.ArrayIndexOutOfBoundsException: 12
This question already has an answer here:
How to prevent ArrayIndexOutOfBoundsException in Java?
23 answers
java arrays
java arrays
asked Jan 18 at 17:19
SoloTriesToLearnSoloTriesToLearn
546
546
marked as duplicate by EJoshuaS, GrumpyCrouton, ekad, Joakim Danielson, M-M Jan 18 at 17:50
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
marked as duplicate by EJoshuaS, GrumpyCrouton, ekad, Joakim Danielson, M-M Jan 18 at 17:50
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
@fantaghirocco what are you talking about "doesn't compile"?
– rustyx
Jan 18 at 17:23
It compiles fine for me...
– Stalemate Of Tuning
Jan 18 at 17:24
DoesnextInt(6)
return a value from 0-5, or 1-6? If anything my guess is you should be subtracting 1 from the result, not adding it.
– Stalemate Of Tuning
Jan 18 at 17:25
@fantaghirocco it compiles for me? Not sure what's going on there
– SoloTriesToLearn
Jan 18 at 17:25
1
two 0's is a 2, and two 5's is a 12
– Stalemate Of Tuning
Jan 18 at 17:37
|
show 6 more comments
@fantaghirocco what are you talking about "doesn't compile"?
– rustyx
Jan 18 at 17:23
It compiles fine for me...
– Stalemate Of Tuning
Jan 18 at 17:24
DoesnextInt(6)
return a value from 0-5, or 1-6? If anything my guess is you should be subtracting 1 from the result, not adding it.
– Stalemate Of Tuning
Jan 18 at 17:25
@fantaghirocco it compiles for me? Not sure what's going on there
– SoloTriesToLearn
Jan 18 at 17:25
1
two 0's is a 2, and two 5's is a 12
– Stalemate Of Tuning
Jan 18 at 17:37
@fantaghirocco what are you talking about "doesn't compile"?
– rustyx
Jan 18 at 17:23
@fantaghirocco what are you talking about "doesn't compile"?
– rustyx
Jan 18 at 17:23
It compiles fine for me...
– Stalemate Of Tuning
Jan 18 at 17:24
It compiles fine for me...
– Stalemate Of Tuning
Jan 18 at 17:24
Does
nextInt(6)
return a value from 0-5, or 1-6? If anything my guess is you should be subtracting 1 from the result, not adding it.– Stalemate Of Tuning
Jan 18 at 17:25
Does
nextInt(6)
return a value from 0-5, or 1-6? If anything my guess is you should be subtracting 1 from the result, not adding it.– Stalemate Of Tuning
Jan 18 at 17:25
@fantaghirocco it compiles for me? Not sure what's going on there
– SoloTriesToLearn
Jan 18 at 17:25
@fantaghirocco it compiles for me? Not sure what's going on there
– SoloTriesToLearn
Jan 18 at 17:25
1
1
two 0's is a 2, and two 5's is a 12
– Stalemate Of Tuning
Jan 18 at 17:37
two 0's is a 2, and two 5's is a 12
– Stalemate Of Tuning
Jan 18 at 17:37
|
show 6 more comments
2 Answers
2
active
oldest
votes
//have to add +1 so I don't get 0
No you really don't.
In Java arrays are 0-based. Meaning an array of 11 elements has indices 0
.. 10
.
Random.nextInt(n)
returns a value between 0
and n-1
.
Therefore the expression ((die1.nextInt(6)+1)+(die2.nextInt(6)+1))
will be between 2
..12
.
Either increase the size of the array to 13 so that 12
fits (the first 2 elements will remain unused), or simply remove +1
's:
Random die1 = new Random(), die2 = new Random();
int counters = new int [11];
for (int i = 0; i <1001; i++){
++counters[die1.nextInt(6)+die2.nextInt(6)];
}
Also you can't print an array with print(). A possible solution is to print each element individually:
for (int i = 0; i < counters.length; ++i) {
System.out.print((i+1) + ":" + counters[i] + " ");
}
System.out.println();
Personally I am not a fan of having empty elements in the array. If you really wanted to keep the mental mapping of dice rolls to values I would prefer to convert the results into a map (preferably unmodifiable) afterwards or something similar.
– Stalemate Of Tuning
Jan 18 at 18:07
add a comment |
You have two dice, and the result of a die roll can be from 0 to 5. I assume the 1001 means you want to roll the two dice 1001 times. Please let me know if I missed anything here.
In this case your min roll would be a 0, and max 10. But, adding 1 to both dice rolls before accessing would mean your index goes from 2 to 12 which causes your ArrayIndexOutOfBoundsException
.
Just don't add anything to the roll. Remember java arrays start from 0, so a range of 0 to 10 is fine.
Quick example:
public static class Die {
static Random rand = new Random();
public static int nextInt(int val) {
return rand.nextInt(val);
}
}
public static void main(String args) {
int counters = new int [11];
for (int i = 0; i <1001; i++){
//no need to add anything
++counters[Die.nextInt(6) + Die.nextInt(6)];
}
System.out.print(Arrays.toString(counters));
}
Example output:
[27, 60, 81, 106, 157, 175, 120, 109, 81, 55, 30]
Which we can translate to mean that a "2" was rolled 27 times, "3" 60 times, etc.
add a comment |
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
//have to add +1 so I don't get 0
No you really don't.
In Java arrays are 0-based. Meaning an array of 11 elements has indices 0
.. 10
.
Random.nextInt(n)
returns a value between 0
and n-1
.
Therefore the expression ((die1.nextInt(6)+1)+(die2.nextInt(6)+1))
will be between 2
..12
.
Either increase the size of the array to 13 so that 12
fits (the first 2 elements will remain unused), or simply remove +1
's:
Random die1 = new Random(), die2 = new Random();
int counters = new int [11];
for (int i = 0; i <1001; i++){
++counters[die1.nextInt(6)+die2.nextInt(6)];
}
Also you can't print an array with print(). A possible solution is to print each element individually:
for (int i = 0; i < counters.length; ++i) {
System.out.print((i+1) + ":" + counters[i] + " ");
}
System.out.println();
Personally I am not a fan of having empty elements in the array. If you really wanted to keep the mental mapping of dice rolls to values I would prefer to convert the results into a map (preferably unmodifiable) afterwards or something similar.
– Stalemate Of Tuning
Jan 18 at 18:07
add a comment |
//have to add +1 so I don't get 0
No you really don't.
In Java arrays are 0-based. Meaning an array of 11 elements has indices 0
.. 10
.
Random.nextInt(n)
returns a value between 0
and n-1
.
Therefore the expression ((die1.nextInt(6)+1)+(die2.nextInt(6)+1))
will be between 2
..12
.
Either increase the size of the array to 13 so that 12
fits (the first 2 elements will remain unused), or simply remove +1
's:
Random die1 = new Random(), die2 = new Random();
int counters = new int [11];
for (int i = 0; i <1001; i++){
++counters[die1.nextInt(6)+die2.nextInt(6)];
}
Also you can't print an array with print(). A possible solution is to print each element individually:
for (int i = 0; i < counters.length; ++i) {
System.out.print((i+1) + ":" + counters[i] + " ");
}
System.out.println();
Personally I am not a fan of having empty elements in the array. If you really wanted to keep the mental mapping of dice rolls to values I would prefer to convert the results into a map (preferably unmodifiable) afterwards or something similar.
– Stalemate Of Tuning
Jan 18 at 18:07
add a comment |
//have to add +1 so I don't get 0
No you really don't.
In Java arrays are 0-based. Meaning an array of 11 elements has indices 0
.. 10
.
Random.nextInt(n)
returns a value between 0
and n-1
.
Therefore the expression ((die1.nextInt(6)+1)+(die2.nextInt(6)+1))
will be between 2
..12
.
Either increase the size of the array to 13 so that 12
fits (the first 2 elements will remain unused), or simply remove +1
's:
Random die1 = new Random(), die2 = new Random();
int counters = new int [11];
for (int i = 0; i <1001; i++){
++counters[die1.nextInt(6)+die2.nextInt(6)];
}
Also you can't print an array with print(). A possible solution is to print each element individually:
for (int i = 0; i < counters.length; ++i) {
System.out.print((i+1) + ":" + counters[i] + " ");
}
System.out.println();
//have to add +1 so I don't get 0
No you really don't.
In Java arrays are 0-based. Meaning an array of 11 elements has indices 0
.. 10
.
Random.nextInt(n)
returns a value between 0
and n-1
.
Therefore the expression ((die1.nextInt(6)+1)+(die2.nextInt(6)+1))
will be between 2
..12
.
Either increase the size of the array to 13 so that 12
fits (the first 2 elements will remain unused), or simply remove +1
's:
Random die1 = new Random(), die2 = new Random();
int counters = new int [11];
for (int i = 0; i <1001; i++){
++counters[die1.nextInt(6)+die2.nextInt(6)];
}
Also you can't print an array with print(). A possible solution is to print each element individually:
for (int i = 0; i < counters.length; ++i) {
System.out.print((i+1) + ":" + counters[i] + " ");
}
System.out.println();
answered Jan 18 at 17:49
rustyxrustyx
29.9k697139
29.9k697139
Personally I am not a fan of having empty elements in the array. If you really wanted to keep the mental mapping of dice rolls to values I would prefer to convert the results into a map (preferably unmodifiable) afterwards or something similar.
– Stalemate Of Tuning
Jan 18 at 18:07
add a comment |
Personally I am not a fan of having empty elements in the array. If you really wanted to keep the mental mapping of dice rolls to values I would prefer to convert the results into a map (preferably unmodifiable) afterwards or something similar.
– Stalemate Of Tuning
Jan 18 at 18:07
Personally I am not a fan of having empty elements in the array. If you really wanted to keep the mental mapping of dice rolls to values I would prefer to convert the results into a map (preferably unmodifiable) afterwards or something similar.
– Stalemate Of Tuning
Jan 18 at 18:07
Personally I am not a fan of having empty elements in the array. If you really wanted to keep the mental mapping of dice rolls to values I would prefer to convert the results into a map (preferably unmodifiable) afterwards or something similar.
– Stalemate Of Tuning
Jan 18 at 18:07
add a comment |
You have two dice, and the result of a die roll can be from 0 to 5. I assume the 1001 means you want to roll the two dice 1001 times. Please let me know if I missed anything here.
In this case your min roll would be a 0, and max 10. But, adding 1 to both dice rolls before accessing would mean your index goes from 2 to 12 which causes your ArrayIndexOutOfBoundsException
.
Just don't add anything to the roll. Remember java arrays start from 0, so a range of 0 to 10 is fine.
Quick example:
public static class Die {
static Random rand = new Random();
public static int nextInt(int val) {
return rand.nextInt(val);
}
}
public static void main(String args) {
int counters = new int [11];
for (int i = 0; i <1001; i++){
//no need to add anything
++counters[Die.nextInt(6) + Die.nextInt(6)];
}
System.out.print(Arrays.toString(counters));
}
Example output:
[27, 60, 81, 106, 157, 175, 120, 109, 81, 55, 30]
Which we can translate to mean that a "2" was rolled 27 times, "3" 60 times, etc.
add a comment |
You have two dice, and the result of a die roll can be from 0 to 5. I assume the 1001 means you want to roll the two dice 1001 times. Please let me know if I missed anything here.
In this case your min roll would be a 0, and max 10. But, adding 1 to both dice rolls before accessing would mean your index goes from 2 to 12 which causes your ArrayIndexOutOfBoundsException
.
Just don't add anything to the roll. Remember java arrays start from 0, so a range of 0 to 10 is fine.
Quick example:
public static class Die {
static Random rand = new Random();
public static int nextInt(int val) {
return rand.nextInt(val);
}
}
public static void main(String args) {
int counters = new int [11];
for (int i = 0; i <1001; i++){
//no need to add anything
++counters[Die.nextInt(6) + Die.nextInt(6)];
}
System.out.print(Arrays.toString(counters));
}
Example output:
[27, 60, 81, 106, 157, 175, 120, 109, 81, 55, 30]
Which we can translate to mean that a "2" was rolled 27 times, "3" 60 times, etc.
add a comment |
You have two dice, and the result of a die roll can be from 0 to 5. I assume the 1001 means you want to roll the two dice 1001 times. Please let me know if I missed anything here.
In this case your min roll would be a 0, and max 10. But, adding 1 to both dice rolls before accessing would mean your index goes from 2 to 12 which causes your ArrayIndexOutOfBoundsException
.
Just don't add anything to the roll. Remember java arrays start from 0, so a range of 0 to 10 is fine.
Quick example:
public static class Die {
static Random rand = new Random();
public static int nextInt(int val) {
return rand.nextInt(val);
}
}
public static void main(String args) {
int counters = new int [11];
for (int i = 0; i <1001; i++){
//no need to add anything
++counters[Die.nextInt(6) + Die.nextInt(6)];
}
System.out.print(Arrays.toString(counters));
}
Example output:
[27, 60, 81, 106, 157, 175, 120, 109, 81, 55, 30]
Which we can translate to mean that a "2" was rolled 27 times, "3" 60 times, etc.
You have two dice, and the result of a die roll can be from 0 to 5. I assume the 1001 means you want to roll the two dice 1001 times. Please let me know if I missed anything here.
In this case your min roll would be a 0, and max 10. But, adding 1 to both dice rolls before accessing would mean your index goes from 2 to 12 which causes your ArrayIndexOutOfBoundsException
.
Just don't add anything to the roll. Remember java arrays start from 0, so a range of 0 to 10 is fine.
Quick example:
public static class Die {
static Random rand = new Random();
public static int nextInt(int val) {
return rand.nextInt(val);
}
}
public static void main(String args) {
int counters = new int [11];
for (int i = 0; i <1001; i++){
//no need to add anything
++counters[Die.nextInt(6) + Die.nextInt(6)];
}
System.out.print(Arrays.toString(counters));
}
Example output:
[27, 60, 81, 106, 157, 175, 120, 109, 81, 55, 30]
Which we can translate to mean that a "2" was rolled 27 times, "3" 60 times, etc.
edited Jan 18 at 17:47
answered Jan 18 at 17:39
Stalemate Of TuningStalemate Of Tuning
452312
452312
add a comment |
add a comment |
@fantaghirocco what are you talking about "doesn't compile"?
– rustyx
Jan 18 at 17:23
It compiles fine for me...
– Stalemate Of Tuning
Jan 18 at 17:24
Does
nextInt(6)
return a value from 0-5, or 1-6? If anything my guess is you should be subtracting 1 from the result, not adding it.– Stalemate Of Tuning
Jan 18 at 17:25
@fantaghirocco it compiles for me? Not sure what's going on there
– SoloTriesToLearn
Jan 18 at 17:25
1
two 0's is a 2, and two 5's is a 12
– Stalemate Of Tuning
Jan 18 at 17:37