-3

I got an 100 random elements array, each element is in range of 0-10, and i need to count each integer how many times it was typed (e.g. 1,2,2,3,8,8,4...)

OUTPUT:

1 - 1

2 - 2

3 - 1

8 - 2

4 - 1

My code so far is:

import java.util.Random;

public class Asses1 {
    public static void main(String[] args) {

        getNumbers();
      }

    private static int randInt() {
        int max = 10;
        int min = 0;

        Random rand = new Random();

        int randomNum = rand.nextInt((max - min) + 1) + min;
        return randomNum;
    }

    public static int[] getNumbers() {
        int number = 100;
        int[] array = new int[number];
        for (int i = 0; i < array.length; i++) {
            System.out.println(randInt());
        }

        System.out.println(number+" random numbers were displayed");
        return array;
    }

}
Aaron
  • 24,009
  • 2
  • 33
  • 57
  • There might be something useful here: http://stackoverflow.com/questions/8098601/java-count-occurrence-of-each-item-in-an-array – Moob Jun 22 '16 at 09:25
  • 1
    Hint: your method getNumbers() ... gets only 0s back. Just **printing** to the console doesn't magically put the numbers into the array. And hint: start small. Dont go for 100 entries initially. Just go for 5 or 10. First make your code work, then go for larger data sets! – GhostCat Jun 22 '16 at 09:27
  • 1
    @Jägermeister Seems like op is unaware of the comments and answers, he was told his getNumbers returns only zeros, still he complaining under the right answer "If i do this output will be: 0 for each element (e.g 0 - 100; 1 - 0 , 2 - 0, 3 - 0 etc)" – eldo Jun 22 '16 at 09:51

4 Answers4

1
int[] array2 = new int[11];

for (int i = 0; i < array.length; i++){
    array2[randInt()]++
}
for (int i = 0; i < array.length; i++)
    System.out.println(String.valueOf(i) + " - " + String.valueOf(array2[i]));

What I have done is:

  1. Create an helping array array2 for storing number of occurences of each number.
  2. When generating numbers increment number of occurences in helping array.
xenteros
  • 15,586
  • 12
  • 56
  • 91
  • 1
    Although people keep upvoting you: you **never** ever should give code only answers. You always EXPLAIN what is going on, too. – GhostCat Jun 22 '16 at 09:31
  • @xenteros The first loop is unnecessary, the array is initialized to zeros. In the second loop, you modify `array` instead of `array2` – AhmadWabbi Jun 22 '16 at 09:37
  • @xenteros OK but you actually don't store the numbers in `array`. How can you print them? – AhmadWabbi Jun 22 '16 at 09:50
  • Easili - for(int i = 0; i < 11;i++) System.out.println(String.valueOf(i) + " " + String.valueOf(array[i])) – xenteros Jun 22 '16 at 09:52
  • @xenteros The `for` loop in your last comment is from 0 to 10, and you print `array[i]`? `array` has the size 100. Also, in your code, you store nothing in `array`. You just use the result of `randInt()` to count. – AhmadWabbi Jun 22 '16 at 10:23
  • @AhmadWabbi lol, I meant array2 of course. Please stop crying on my correct answer. – xenteros Jun 22 '16 at 10:29
  • @xenteros Look, you really don't get what I am saying. I know your answer is correct, but it is missing something: How to print the original `array`? When I ask you about printing `array`, you answer by printing `array2`. Just be more careful when you read other's comments. The original program needs to print them, as you can see in the question. – AhmadWabbi Jun 22 '16 at 12:58
1

Add this method, which will do the counting:

public static void count(int[] x) {
    int[] c=new int[11];

    for(int i=0; i<x.length; i++)
        c[x[i]]++;

    for(int i=0; i<c.length; i++)
        System.out.println(i+" - "+c[i]);
}

and change the main into this so that you call the previous method:

public static void main(String[] args) {

    count(getNumbers());
  }

Also, change the for loop in getNumbers into this in order to fill array with the generated numbers, not just printing them:

    for (int i = 0; i < array.length; i++) {
        array[i] = randInt();
        System.out.println(array[i]);
    }
AhmadWabbi
  • 2,253
  • 1
  • 20
  • 35
  • If i do this output will be: 0 for each element (e.g 0 - 100; 1 - 0 , 2 - 0, 3 - 0 etc) – Tudor Velcescu Jun 22 '16 at 09:32
  • This is because you do not do the last modification in the loop of `getNumbers`. Do it and check the correctness. The array you return from `getNumbers` is all zeros, so the result is logic. – AhmadWabbi Jun 22 '16 at 09:34
1

Here is how it can be done in java 8

// Retrieve the random generated numbers
int[] numbers = getNumbers();
// Create an array of counters of size 11 as your values go from 0 to 10
// which means 11 different possible values.
int[] counters = new int[11];
// Iterate over the generated numbers and for each number increment 
// the counter that matches with the number
Arrays.stream(numbers).forEach(value -> counters[value]++);
// Print the content of my array of counters
System.out.println(Arrays.toString(counters));

Output:

[12, 11, 7, 6, 9, 12, 8, 8, 10, 9, 8]

NB: Your method getNumbers is not correct you should fix it as next:

public static int[] getNumbers() {
    int number = 100;
    int[] array = new int[number];
    for (int i = 0; i < array.length; i++) {
        array[i] = randInt();
    }

    System.out.println(number+" random numbers were displayed");
    return array;
}
Nicolas Filotto
  • 43,537
  • 11
  • 94
  • 122
  • He didn't even understand that he has to STORE the values in his array (as he just printed them). And you think he has any chance of understanding this code, without any explanations whatsoever. Hmm, unlikely I would say. – GhostCat Jun 22 '16 at 09:33
-1
Map<Integer,Integer> map=new HashMap<Integer,Integer>();
        int temp;
        for (int i = 0; i < array.length; i++) {
            temp=randInt();
            if(map.containsKey(temp)){
                map.put(temp, map.get(temp)+1);
            }else{
                map.put(temp, 1);
            }
        }
Dipak Prajapati
  • 486
  • 5
  • 13
  • Please note: good answers **always** come with explanations; not just code. Especially when you are talking to a newbie; you really can't expect that he is able to "get things" just by reading code only. – GhostCat Jun 22 '16 at 10:35