Monocyte
Monocyte

Reputation: 95

Counting Occurrences With An Array - Java

I'm lost as how to compare 100 randomly generated number between 0-9 to an array value, also between 0-9, and then print the results. Be easy on me, I'm new to coding and I know I suck. I feel as though I'm 75% there. I know there are ways to make some of the code less redundant, however I seem to struggle with those techniques.

Here's what I have so far:

public static void main(String[] args) {
    double randomNum = 0;
    for (int i = 0; i < 100; i++) {
        randomNum = Math.random() * 10;

    int count0 = 0, count1 = 0, count2 = 0, count3 = 0, count4 = 0;
    int count5 = 0, count6 = 0, count7 = 0, count8 = 0, count9 = 0;
    int [] arrayNums = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
    for (double j = 0; j <arrayNums.length; j++){
        if (arrayNums[0] == randomNum) {
            count0++;
        }
        else if (arrayNums[1] == randomNum){
            count1++;
        }
        else if (arrayNums[2] == randomNum){
            count2++;
        }else if (arrayNums[3] == randomNum){
            count3++;
        }else if (arrayNums[4] == randomNum){
            count4++;
        }else if (arrayNums[5] == randomNum){
            count5++;
        }else if (arrayNums[6] == randomNum){
            count6++;
        }else if (arrayNums[7] == randomNum){
            count7++;
        }else if (arrayNums[8] == randomNum){
            count8++;
        }
        else{
            count9++;
        }

    }
    System.out.print("Occurrences of 0: " + count0);
    System.out.print("\nOccurrences of 1: " + count1);
    System.out.print("\nOccurrences of 2: " + count2);
    System.out.print("\nOccurrences of 3: " + count3);
    System.out.print("\nOccurrences of 4: " + count4);
    System.out.print("\nOccurrences of 5: " + count5);
    System.out.print("\nOccurrences of 6: " + count6);
    System.out.print("\nOccurrences of 7: " + count7);
    System.out.print("\nOccurrences of 8: " + count8);
    System.out.print("\nOccurrences of 9: " + count9);
    }
}

}

Any and all help is appreciated.

Upvotes: 3

Views: 124

Answers (5)

Nir Alfasi
Nir Alfasi

Reputation: 53525

  • Using a HashMap as a counter is more elegant than keeping a separate variable for each item.
  • Use Random.nextInt(10) to generate a random number between 0 to 9 (inclusive).

        Map<Integer, Integer> counter = new HashMap<Integer, Integer>();
        Random rand = new Random();
        for (int i = 0; i < 100; i++) {
            int randomNum = rand.nextInt(10);
            Integer currentCount = counter.get(randomNum);
            if (null == currentCount) {
                counter.put(randomNum, 1);
            } else {
                counter.put(randomNum, currentCount+1);
            }
        }
        for (Integer key : counter.keySet()) {
            System.out.println(key + " -> " + counter.get(key));
        }
    

Example output

0 -> 12
1 -> 10
2 -> 9
3 -> 6
4 -> 8
5 -> 9
6 -> 11
7 -> 12
8 -> 14
9 -> 9

Upvotes: 2

Vitaly
Vitaly

Reputation: 2662

Well, instead of using

int count0 = 0, count1 = 0, count2 = 0, count3 = 0, count4 = 0;
int count5 = 0, count6 = 0, count7 = 0, count8 = 0, count9 = 0;

You can use:

int[] count = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0};

And your method will look like:

  public static void main(String[] args) {
        int[] count = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
        int[] arrayNums = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
        for (int i = 0; i < 100; i++) {
            int randomNum = (int) (Math.random() * 10);
            for (int j = 0; j < 10; j++) {
                if (arrayNums[j] == randomNum) {
                    count[j]++;
                }
            }
        }
        for (int i = 0; i < 10; i++) {
            System.out.println("Occurrences of " + i + ": " + count[i]);
        }
    }

Upvotes: 1

Randyka Yudhistira
Randyka Yudhistira

Reputation: 3652

You call the random in wrong way. it should be like this :

Random rnd = new Random();
int randomNum = rnd.nextInt(10);

And you put count variable in wrong way too. Then your full code will be like this :

public static void main(String[] args) {
    Random random = new Random();
    int count0 = 0, count1 = 0, count2 = 0, count3 = 0, count4 = 0;
    int count5 = 0, count6 = 0, count7 = 0, count8 = 0, count9 = 0;
    for (int i = 0; i < 100; i++) {
        int randomNum = random.nextInt(10);


        int [] arrayNums = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
        for (double j = 0; j <arrayNums.length; j++){
          if (arrayNums[0] == randomNum) {
            count0++;
          }
          else if (arrayNums[1] == randomNum){
            count1++;
          }
          else if (arrayNums[2] == randomNum){
            count2++;
          }else if (arrayNums[3] == randomNum){
            count3++;
          }else if (arrayNums[4] == randomNum){
            count4++;
          }else if (arrayNums[5] == randomNum){
            count5++;
          }else if (arrayNums[6] == randomNum){
            count6++;
          }else if (arrayNums[7] == randomNum){
            count7++;
          }else if (arrayNums[8] == randomNum){
            count8++;
          }
          else{
            count9++;
          }

        }

    }
    System.out.print("Occurrences of 0: " + count0);
    System.out.print("\nOccurrences of 1: " + count1);
    System.out.print("\nOccurrences of 2: " + count2);
    System.out.print("\nOccurrences of 3: " + count3);
    System.out.print("\nOccurrences of 4: " + count4);
    System.out.print("\nOccurrences of 5: " + count5);
    System.out.print("\nOccurrences of 6: " + count6);
    System.out.print("\nOccurrences of 7: " + count7);
    System.out.print("\nOccurrences of 8: " + count8);
    System.out.print("\nOccurrences of 9: " + count9);
}

Upvotes: 0

Ruchira Gayan Ranaweera
Ruchira Gayan Ranaweera

Reputation: 35547

First add all values to array. Then iterate the array and store the value as key in a HashMap and values as a count of occurrences. Then after iteration you will have what you trying to archive here.

Eg:

 int[] arr = {0, 1, 0, 2, 5, 2, 4, 6, 0, 4, 7, 8, 9, 0, 2, 5, 7, 6};
 Map<Integer, Integer> countMap = new HashMap<>();
  for (int i : arr) {
    Integer currentCount = countMap.get(i);
    if (currentCount != null) { 
       countMap.put(i, currentCount + 1);
    } else {
       countMap.put(i, 1);
    }
  }
 for(Map.Entry<Integer,Integer> entry:countMap.entrySet()){
        System.out.println("Number of occurrences of "+entry.getKey()
                                                   +" : "+entry.getValue());
 }

Upvotes: 1

Shaun Frisbee
Shaun Frisbee

Reputation: 94

You are resetting your counters back to 0 every iteration of the for loop. And I don't know what the second loop is for.

    public static void main(String[] args) {
    double randomNum = 0;

    int count0 = 0, count1 = 0, count2 = 0, count3 = 0, count4 = 0;
    int count5 = 0, count6 = 0, count7 = 0, count8 = 0, count9 = 0;

    for (int i = 0; i < 100; i++) {
        randomNum = Math.random() * 10;

        switch(randomNum) {
            case 0: count0++; break;
            case 1: count1++; break;
            case 2: count2++; break;
            case 3: count3++; break;
            case 4: count4++; break;
            case 5: count5++; break;
            case 6: count6++; break;
            case 7: count7++; break;
            case 8: count8++; break;
            case 9: count9++; break;
        }

    }


    System.out.print("Occurrences of 0: " + count0);
    System.out.print("\nOccurrences of 1: " + count1);
    System.out.print("\nOccurrences of 2: " + count2);
    System.out.print("\nOccurrences of 3: " + count3);
    System.out.print("\nOccurrences of 4: " + count4);
    System.out.print("\nOccurrences of 5: " + count5);
    System.out.print("\nOccurrences of 6: " + count6);
    System.out.print("\nOccurrences of 7: " + count7);
    System.out.print("\nOccurrences of 8: " + count8);
    System.out.print("\nOccurrences of 9: " + count9);
}

Upvotes: 0

Related Questions