Search code examples
javasimulationdice

How to read in a list of numbers 1-6 and count the repeated numbers java


So I am trying to read in from a file that has upwards of 10000000 numbers in it that range from 1-6. Essentially the point of this exercise is to simulate a dice being rolled and I need to find the probability of each number being rolled. I wrote code to write random numbers 1-6 to a file which is:

public static void main(String[]args)throws IOException{
    Scanner kb = new Scanner(System.in);
    PrintWriter pw = new PrintWriter(new File("Rolls.txt"));
    System.out.println("Input a number for the amount of rolls");

    long rolls = kb.nextLong();

    for(int i = 0; i< rolls; i++){
        
        if(i % 20 == 0){
            pw.println();
        }
        else{
        pw.print(((int)(Math.random()*6)+1) + ",");
        }
    }


   
    pw.close();
}

So using this file I suppose I am supposed to read in an entire line as a string, then split the line at the comma and then change the strings to longs/ints. What I'm struggling with is that I'm supposed to count the number of 1's .... 6's and store that number into another array, and the take each element of that array and find the probability of it getting rolled. The code I have this far for this second part is below. I tried some things but nothing worked and I'm stumped.

public static void main(String[]args) throws IOException{
    Scanner in = new Scanner(new File("Rolls.txt"));
    long[] list = {0,0,0,0,0,0};
   
    while ( in.hasNextLine()){
        String line = in.nextLine();
        String[] numbers = line.split(",");
       
       
    }

Any help is appreciated. Thanks!


Solution

  • The code you used to generate the rolls didn't do very well. It put an empty new line at the start of the file because i was equal to 0 in the first iteration of the cycle. The second problem was that the number of generated rolls was less that the amount you wanted because on some iterations you wrote a new line only and no number. This is how I modified your code to generate the rolls correctly.

    public static void main(String[]args)throws IOException{
        Scanner kb = new Scanner(System.in);
        PrintWriter pw = new PrintWriter(new File("Rolls.txt"));
        System.out.println("Input a number for the amount of rolls");
    
        long rolls = kb.nextLong();
    
        for(int i = 1; i<= rolls; i++){
    
            if(i % 20 == 0){
                pw.println();
            }
            pw.print(((int)(Math.random()*6)+1) + ",");
        }
    
    
    
        pw.close();
    }
    

    Now here is the continuation of your code for reading and calculating probabilities. As some people have already commented you should use Integer.parseInt(); to get the string values as integers.

            public static void main(String[]args) throws IOException{
                    Scanner in = new Scanner(new File("Rolls.txt"));
                    long[] list = {0,0,0,0,0,0};
                    while ( in.hasNextLine()){
                            String line = in.nextLine();
                            String[] numbers = line.split(",");
                            //parse the string to ints
                            for(int i=0;i<numbers.length;i++){
                                    int currentValue = Integer.parseInt(numbers[i]);
                                    list[currentValue-1]++;
                            }
                    }
                    long totalRollCount=0;
                    for(int i=0;i<list.length;i++){
                            totalRollCount+=list[i];
                    }
                    System.out.println(totalRollCount);
                    for(int i=0;i<list.length;i++){
                            System.out.println((i+1)+" "+((double)list[i]/totalRollCount));
                    }
        }