# Thread: If statements and arrays, problem

1. Member
Join Date
Nov 2011
Posts
2
Rep Power
0

## If statements and arrays, problem

Hello, just started with JAVA programming and need some help.
I'm from sweden and its swedish if you dont understand some sentences.
Its a start for yatzy and i know it is not optimized , so to speak. But i would like to advance in my own speed but would gladly get help with this problem.

Java Code:
```public class HEj {
public static void main(String[] args){
int[] Tarning = new int[5];
//Slå 5
Tarning[0] = (int)(Math.random() * 6) +1;
Tarning[1] = (int)(Math.random() * 6) +1;
Tarning[2] = (int)(Math.random() * 6) +1;
Tarning[3] = (int)(Math.random() * 6) +1;
Tarning[4] = (int)(Math.random() * 6) +1;
System.out.println("Du slog: "+ Tarning[0] + " " + Tarning[1] + " " + Tarning[2] + " " + Tarning[3] + " " + Tarning[4]);
System.out.println("Hur många vill du kasta om?");
int antal = 0;
if (antal==0){
System.out.println("Du ska skriva \"0\" i nästa fråga.");
}
System.out.println("Slå om " + antal + " av tärningarna, ange med 1 ,2 ,3 ,4 eller 5.");
//Antal 2 anger hur många gånger "vilka" ska köras.
int antal2 = antal;
int[] vilka = new int [antal];
if (antal2 > 0){
antal2 = antal2 - 1;
}
if (antal2 > 0){
antal2 = antal2 - 1;
}
if (antal2 > 0){
antal2 = antal2 - 1;
}
if (antal2 > 0){
antal2 = antal2 - 1;
}
if (antal2 > 0){
antal2 = antal2 - 1;
}
if (vilka[0]==1){
Tarning[0] = (int)(Math.random() * 6) +1;
}
if (vilka[0]==2){
Tarning[1] = (int)(Math.random() * 6) +1;
}
if (vilka[0]==3){
Tarning[2] = (int)(Math.random() * 6) +1;
}
if (vilka[0]==4){
Tarning[3] = (int)(Math.random() * 6) +1;
}
if (vilka[0]==5){
Tarning[4] = (int)(Math.random() * 6) +1;
}
if (vilka[1]==2){
Tarning[1] = (int)(Math.random() * 6) +1;
}
if (vilka[1]==3){
Tarning[2] = (int)(Math.random() * 6) +1;
}
if (vilka[1]==4){
Tarning[3] = (int)(Math.random() * 6) +1;
}
if (vilka[1]==5){
Tarning[4] = (int)(Math.random() * 6) +1;
}
if (vilka[2]==3){
Tarning[2] = (int)(Math.random() * 6) +1;
}
if (vilka[2]==4){
Tarning[3] = (int)(Math.random() * 6) +1;
}
if (vilka[2]==5){
Tarning[4] = (int)(Math.random() * 6) +1;
}
if (vilka[3]==4){
Tarning[3] = (int)(Math.random() * 6) +1;
}
if (vilka[3]==5){
Tarning[4] = (int)(Math.random() * 6) +1;
}
if (vilka[4]==5){
Tarning[4] = (int)(Math.random() * 6) +1;
}
System.out.println("Du slog: " + Tarning[0] + " " + Tarning[1] + " " + Tarning[2] + " " + Tarning[3] + " " + Tarning[4] + ".");
}
}```

2. ## Re: If statements and arrays, problem

Do you get errors? Is so please copy and paste the full text of the message here.

3. Member
Join Date
Nov 2011
Posts
2
Rep Power
0

## Re: If statements and arrays, problem

First the program asks how many you wanna cast again(dices)
If antal<5 the program gets this error message
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException:3 //(Here is the number you give to antal)
at HEj.main(HEj.java:77) //Here you go to the line "if (vilka[1]==2){"

4. ## Re: If statements and arrays, problem

.ArrayIndexOutOfBoundsException:3
Your are trying to use an index that is past the end of the array.
Look at line 77 in your code and see why the index used on that line is past the end of the array.