# Thread: Counting Occurences from Inputted Integers HELP!

1. Member
Join Date
Sep 2013
Posts
5
Rep Power
0

## Counting Occurences from Inputted Integers HELP!

[B]Hi all! I'm new to java and this is one of our final requirements in school: 1 dimensional arrays. Here's the code and I commented the part I'm stuck in. Kindly help anyone!

PROBLEM DEFINITION: Ask the user how many numbers he would like to input. Accept numbers and store in an array. Maximum of 20 numbers only. Access the array and count the occurences of each inputted integer. Print in a table labeled NUMBERS and OCCURENCE in separate columns.

Here's my code:

Last edited by ProgrammingNoob; 09-27-2013 at 12:19 PM.

2. Just a guy
Join Date
Jun 2013
Location
Netherlands
Posts
5,114
Rep Power
13

## Re: Counting Occurences from Inputted Integers HELP!

I must say that this is a nice challenging assignment. It would be a waste if someone would be telling you what to do to solve it, you won't learn anything in the process. The most important thing to learn in programming is the way to think, not the actual code itself. Once you can think in the right way, the code comes naturally.

Given that statement: you must have some idea of what to do, even when you can't figure out how exactly to do it in code just yet. So share what you have come up with yourself so far.

3. Member
Join Date
Sep 2013
Posts
5
Rep Power
0

## Re: Counting Occurences from Inputted Integers HELP!

Hi thanks for replying! Here's what I'm thinking of but can't write in code! XD

1. Access the elements of the array and compare the previous index with the elements of all the other indeces.
2. Find a match of equal numbers and increment a counter.
3. Print the elements just once each and print their occurence via the counter.

That's it so far. I just can't seem to write 'em in code!

4. Member
Join Date
Sep 2013
Posts
5
Rep Power
0

## Re: Counting Occurences from Inputted Integers HELP!

This is all the algorithm I can muster for the accessing, counting and printing part: I don't know what to do anymore honestly! Been learning java for 3 months now and it's the first time I'm stuck!

5. ## Re: Counting Occurences from Inputted Integers HELP!

You forgot something. Do it with pen and paper and pay attention to what you are actually doing. Your three steps are not detailed enough to be converted into code.

6. Member
Join Date
Sep 2013
Posts
5
Rep Power
0

## Re: Counting Occurences from Inputted Integers HELP!

1. Access the elements of the array by creating a for loop.
2. Compare the elements of the array with each other to find a match.
3. Create counter.
4. If there is a match, increment the counter.
5. Counter must reset and save number of counts
5. Continue comparing
6. If there is another unique match, increment the counter again.
7. Counter mus reset and save number of counts
8. Ends when all array elements are compared with each other
9. Access array by creating a for loop.
10. Single-out unique elements.
11. Print out the unique elements.
12. Print out their occurrences.

Is what I'm trying to do.

7. ## Re: Counting Occurences from Inputted Integers HELP!

Ok, what I would try to do is the following:
Java Code:
```1) create array as big as the original one
2) loop over original array
2.1) if the current number is not in new array insert number of original into new array
3) new array only cointains each individual number once
4) loop over new array
4.1) loop over original
4.2) increment counter each time the numbers match
4.3) display number and counter
4.4) reset counter for next number
5) You're done```

8. Member
Join Date
Sep 2013
Posts
5
Rep Power
0

## Re: Counting Occurences from Inputted Integers HELP!

Thank you!

So far I've come up with this based on your algorithm. I know what I want to do as you've already told me but I can't seem to get the result.

Java Code:
```import java.util.*;
public class CaseStudy1{
public static void main (String [] reyben){
Scanner scan = new Scanner(System.in);
int numinput = 0, counter = 1;

System.out.println("How many numbers do you like to input?: ");
numinput = scan.nextInt();

//only 20 numbers max inputs
if (numinput>20){
System.out.println("Maximum is 20 numbers only!");
}
else {

//below 20, ask for inputs and store in array
System.out.println("Enter " + numinput + " numbers: ");
int numbers[] = new int [numinput];
for (int i = 0; i<numbers.length; i++){
numbers[i] = scan.nextInt();
}

//new array
int newArray[] = new int [numbers.length];

// loop original array and store unique numbers in new array
for (int i=0; i<numbers.length; i++){
if (numbers[i]!= newArray[i]){
newArray[i] = numbers[i];
}

//loop over original, increment counter when there is match, print out, then reset counter
counter = 1;
for (i = 0; i<newArray.length; i++){
for (int j=0; j<numbers.length; j++){
if (newArray[i]==numbers[i]){
counter++;
}
System.out.println(numbers[i] + "\t\t\t" + counter);
}
}

}

}
}
}```
Last edited by ProgrammingNoob; 09-27-2013 at 02:42 PM.

9. Just a guy
Join Date
Jun 2013
Location
Netherlands
Posts
5,114
Rep Power
13

## Re: Counting Occurences from Inputted Integers HELP!

don't attach the code or post a picture, just post the code itself here and use code tags to pretty-format it. The advanced post editor has a button to do that.

10. ## Re: Counting Occurences from Inputted Integers HELP!

1) Where does the for loop beginning in line 28 end?
2) Check line 29: does this really test whether the number isn't anywhere in newArray?
3) Where is point 4.4 from my pseudocode?
4) What happens if the user enters a letter instead of a number?

11. Just a guy
Join Date
Jun 2013
Location
Netherlands
Posts
5,114
Rep Power
13

## Re: Counting Occurences from Inputted Integers HELP!

Originally Posted by PhHein
4) What happens if the user enters a letter instead of a number?
Or 0, or -1000. Both valid integers and not higher than 20.