import java.util.ArrayList; import java.util.Scanner; public class CharacterFrequency { public static void arrayDigits(String number){ int size = number.length(); ArrayList<Integer> result = new ArrayList<Integer>(size); for (int i = 0; i < number.length(); i++){ int element = number.charAt(i) - '0'; result.add(element); } countDigits(result); } public static void countDigits(ArrayList<Integer> array){ int size = array.size(); for (int i = 0; i < size; i++){ int position = i; int count = 0; for (int j = 0; j < size; j++){ int element = array.get(j); if (position == element){ count++; array.set(j, count); } } System.out.println("The number of " + i + " is " + array.get(i)); } } public static void main(String[] args) { Scanner keyboard = new Scanner(System.in); System.out.println("Please enter your phone number, area code first"); String phoneNumber = keyboard.next(); arrayDigits(phoneNumber); } }

## 11 Replies - 9931 Views - Last Post: 30 January 2013 - 08:31 AM

### #1

# Count frequency of numbers in a phone number using ArrayList

Posted 28 January 2013 - 08:43 PM

##
**Replies To:** Count frequency of numbers in a phone number using ArrayList

### #2

## Re: Count frequency of numbers in a phone number using ArrayList

Posted 29 January 2013 - 01:38 AM

int[] phoneNumber = {5,5,5,3,3,3,1,2,1,2}; int[] frequency = new int[10]; for(int number : phoneNumber) { ++frequency[number]; } int count = 0; for (int freq : frequency) { System.out.println(count + " occurs "+ freq + " times."); count++; }

This post has been edited by **farrell2k**: 30 January 2013 - 03:52 PM

### #3

## Re: Count frequency of numbers in a phone number using ArrayList

Posted 29 January 2013 - 06:08 AM

farrell2k, on 29 January 2013 - 02:38 AM, said:

int[] phoneNumber = {5,5,5,3,3,3,1,2,1,2}; int[] frequency = new int[phoneNumber.length]; for(int number : phoneNumber) { ++frequency[number]; } int count = 0; for (int freq : frequency) { System.out.println(count + " occurs "+ freq + " times."); count++; }

I know how to do it that way, which i have, but I need to do it using ArrayList.

### #4

## Re: Count frequency of numbers in a phone number using ArrayList

Posted 29 January 2013 - 08:48 AM

### #5

## Re: Count frequency of numbers in a phone number using ArrayList

Posted 29 January 2013 - 08:59 AM

farrell2k, on 29 January 2013 - 08:38 AM, said:

int[] phoneNumber = {5,5,5,3,3,3,1,2,1,2}; int[] frequency = new int[phoneNumber.length]; for(int number : phoneNumber) { ++frequency[number]; } int count = 0; for (int freq : frequency) { System.out.println(count + " occurs "+ freq + " times."); count++; }

It should be

int[] frequency = new int[10];

Instead of

int[] frequency = new int[phoneNumber.length];

You really should be able to take his code, and just replace the array with an ArrayList You would then need to use the get and set methods of ArrayList

This post has been edited by **CasiOo**: 29 January 2013 - 09:01 AM

### #6

## Re: Count frequency of numbers in a phone number using ArrayList

Posted 29 January 2013 - 09:04 AM

CasiOo, on 29 January 2013 - 03:59 PM, said:

farrell2k, on 29 January 2013 - 08:38 AM, said:

int[] phoneNumber = {5,5,5,3,3,3,1,2,1,2}; int[] frequency = new int[phoneNumber.length]; for(int number : phoneNumber) { ++frequency[number]; } int count = 0; for (int freq : frequency) { System.out.println(count + " occurs "+ freq + " times."); count++; }

It should be

int[] frequency = new int[10];

Instead of

int[] frequency = new int[phoneNumber.length];

I do not understand why.

### #7

## Re: Count frequency of numbers in a phone number using ArrayList

Posted 29 January 2013 - 09:08 AM

8 and 9 are out of bounds

public class Snippet { /** * @param args */ public static void main(String[] args) { int[] phoneNumber = { 1, 2, 3, 4, 1, 5, 8, 9 }; int[] frequency = new int[phoneNumber.length]; for(int number : phoneNumber) { ++frequency[number]; } int count = 0; for (int freq : frequency) { System.out.println(count + " occurs "+ freq + " times."); count++; } } }

Here they are not

public class Snippet { /** * @param args */ public static void main(String[] args) { int[] phoneNumber = { 1, 2, 3, 4, 1, 5, 8, 9 }; int[] frequency = new int[10]; for(int number : phoneNumber) { ++frequency[number]; } int count = 0; for (int freq : frequency) { System.out.println(count + " occurs "+ freq + " times."); count++; } } }

### #8

## Re: Count frequency of numbers in a phone number using ArrayList

Posted 29 January 2013 - 09:20 AM

farrell2k, on 29 January 2013 - 09:48 AM, said:

I think I have use the .set option after creating the frequency array.

so it would have to be something like

ArrayList<Integer>frequency = new ArrayList<Integer>(size); int count = 0; for (int element: originalArray){ frequency.set(element, count++); }

i think.

i know how to do it when building an array using int [] whatever = new int[10], etc. but just not using ArrayList.

### #9

## Re: Count frequency of numbers in a phone number using ArrayList

Posted 29 January 2013 - 09:23 AM

ArrayList<Integer> number... int[] frequency = new int[10]; //thanks CasiOo. for(int number : number) { ++frequency[number]; } int count = 0; for (int freq : frequency) { System.out.println(count + " occurs "+ freq + " times."); count++; }

Unless I am way off in my thinking, which is completely possible, this will work.

This post has been edited by **farrell2k**: 30 January 2013 - 03:53 PM

### #10

## Re: Count frequency of numbers in a phone number using ArrayList

Posted 29 January 2013 - 09:31 AM

dfl2m, on 29 January 2013 - 04:20 PM, said:

farrell2k, on 29 January 2013 - 09:48 AM, said:

I think I have use the .set option after creating the frequency array.

so it would have to be something like

ArrayList<Integer>frequency = new ArrayList<Integer>(size); int count = 0; for (int element: originalArray){ frequency.set(element, count++); }

i think.

i know how to do it when building an array using int [] whatever = new int[10], etc. but just not using ArrayList.

Almost. The ArrayList should contain how many times a number has occurred

So if the number 1 occurs 5 times, the follow should be true

frequency.get(1) == 5

Will this be the case with your own code? No!

You are using the same count variable for all the numbers

What you are currently counting is how many numbers there are, not the individual count of them =o

I think farrell2k is drunk right now, he doesn't seem like his normal self right now

### #11

## Re: Count frequency of numbers in a phone number using ArrayList

Posted 29 January 2013 - 09:32 AM

CasiOo, on 29 January 2013 - 10:29 AM, said:

dfl2m, on 29 January 2013 - 04:20 PM, said:

farrell2k, on 29 January 2013 - 09:48 AM, said:

I think I have use the .set option after creating the frequency array.

so it would have to be something like

ArrayList<Integer>frequency = new ArrayList<Integer>(size); int count = 0; for (int element: originalArray){ frequency.set(element, count++); }

i think.

i know how to do it when building an array using int [] whatever = new int[10], etc. but just not using ArrayList.

Almost. The ArrayList should contain how many times a number has occurred

So if the number 1 occurs 5 times, the follow should be true

frequency.get(1) == 5

Will this be the case with your own code? No!

You are using the same count variable for all the numbers

What you are currently counting is how many numbers there are, not the individual count of them =o

okay, i will have to look at my code later b/c i'm at work.

### #12

## Re: Count frequency of numbers in a phone number using ArrayList

Posted 30 January 2013 - 08:31 AM

for (int i = 0; i < frequencySize; i++){ int count = 0; //reset this after every iteration of the outer loop for (int j = 0; j < arraySize; j++){ if (i == array.get(j)){ //for example, if i = 0, and array.get(j) = 0, then count++; //incremement count by 1 frequency.set(i, count); // set frequency(0, 1) } } System.out.println("The number of " + i + " is " + frequency.get(i)); }