I have quite the trouble figuring this out, I've tried different methods and workarounds but i think i'm too afraid to even touch the code for the amount of errors i manage to get every time!
The issue is that i'm trying to simply read from an char array which currently has 1000 randomly generated numbers in each element. I need to show statistics over this String/chars values which ranges between 1-6 and store it into an statistics array, then print it out.. Where am i doing wrong?
Heres a snippet of my code:
public static int[] showGame(String tempResult) throws IOException{
int statistics [] = new int [6];
char [] resultat = new char[1000];
String tempStr;
try{
BufferedReader indata = new BufferedReader(new FileReader("DiceGame.txt"));
tempStr = indata.toString();
char result [] = tempStr.toCharArray();
for (int i = 0; i < 1000; i++) {
resultat[i] = tempStr.charAt(i);
switch (result[i]) {
case '1':
statistics[0]++;
break;
case '2':
statistics[1]++;
break;
case '3':
statistics[2]++;
break;
case '4':
statistics[3]++;
break;
case '5':
statistics[4]++;
break;
case '6':
statistics[5]++ ;
break;
}
}
} catch (IOException ex) {
JOptionPane.showMessageDialog(null, "An error occured: " + ex);
}catch (NumberFormatException e){
JOptionPane.showMessageDialog(null,"Error: " + e);
}
//Arrays.toString(statistics);
return statistics;
}
EDIT: OR could it be because i'm not calling it properly from main?!
public static void main(String[] args) throws IOException {
int []Statistik = new int[6];
String tossValue = JOptionPane.showInputDialog("set value for amount f toss");
DiceWithArray.Game(tossValue); //Kasta in värdet in i klass metoden
String tempToss = DiceWithArray.Game(tossValue);
Statistik = DiceWithArray.showGame(tempToss);
System.out.println("Resultatet : " + Arrays.toString(Statistik));