-1

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));
Patt089
  • 53
  • 1
  • 1
  • 6
  • 1
    `BufferedReader.toString` does not do what you think it does. You need to use the `BufferedReader` to read the file via `read` or `readLine` and make a char array out of it. – Zircon Sep 21 '16 at 18:07
  • What does your debugger of indata say? Is it what you expected? does the length line up with what you want? It probably won't be to any of these. Also. in general it is bad practice to hardcode that 1000 if you intend to read the whole file use .length – CodeMonkey Sep 21 '16 at 18:11

1 Answers1

0

You do:

for (int i = 0; i < 1000; i++) {       
  resultat[i] = tempStr.charAt(i);

and well, you can't be sure that tempStr must be 1000 chars or longer. Do something like thta:

for (int i = 0; i < 1000; i++) {    
  if(tempStr.length() <= i)
    break;
  resultat[i] = tempStr.charAt(i);

And you can't just use toString on BufferedReader, read the whole file by nextLine method, then do things with it.

Shadov
  • 5,421
  • 2
  • 19
  • 38