11 Replies - 3819 Views - Last Post: 30 January 2013 - 08:31 AM Rate Topic: -----

#1 dfl2m  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 05-December 12

Count frequency of numbers in a phone number using ArrayList

Posted 28 January 2013 - 08:43 PM

I'm trying to write a program that will count the frequency of numbers in a phone number using ArrayList. The phone number must be input as a String. I think I have it right, but the counts are coming out messed up. Attached is my code:

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);

	}

}



Is This A Good Question/Topic? 0
  • +

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

#2 farrell2k  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 823
  • View blog
  • Posts: 2,536
  • Joined: 29-July 11

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

Posted 29 January 2013 - 01:38 AM

Here's a simple way to count the number of times a digit shows up in an array.

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

Was This Post Helpful? 0
  • +
  • -

#3 dfl2m  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 05-December 12

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

Posted 29 January 2013 - 06:08 AM

View Postfarrell2k, on 29 January 2013 - 02:38 AM, said:

Here's a simple way to count the number of times a digit shows up in an array.

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.
Was This Post Helpful? 0
  • +
  • -

#4 farrell2k  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 823
  • View blog
  • Posts: 2,536
  • Joined: 29-July 11

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

Posted 29 January 2013 - 08:48 AM

You have an array of integers and you can't imagine how to substitute its values instead???
Was This Post Helpful? 0
  • +
  • -

#5 CasiOo  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 1391
  • View blog
  • Posts: 3,077
  • Joined: 05-April 11

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

Posted 29 January 2013 - 08:59 AM

View Postfarrell2k, on 29 January 2013 - 08:38 AM, said:

Here's a simple way to count the number of times a digit shows up in an array.

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

Was This Post Helpful? 1
  • +
  • -

#6 farrell2k  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 823
  • View blog
  • Posts: 2,536
  • Joined: 29-July 11

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

Posted 29 January 2013 - 09:04 AM

View PostCasiOo, on 29 January 2013 - 03:59 PM, said:

View Postfarrell2k, on 29 January 2013 - 08:38 AM, said:

Here's a simple way to count the number of times a digit shows up in an array.

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. :unsure:
Was This Post Helpful? 0
  • +
  • -

#7 CasiOo  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 1391
  • View blog
  • Posts: 3,077
  • Joined: 05-April 11

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

Posted 29 January 2013 - 09:08 AM

This could have been my phone number
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++;
    }
	}

}



Was This Post Helpful? 2
  • +
  • -

#8 dfl2m  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 05-December 12

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

Posted 29 January 2013 - 09:20 AM

View Postfarrell2k, on 29 January 2013 - 09:48 AM, said:

You have an array of integers and you can't imagine how to substitute its values instead???



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.
Was This Post Helpful? 0
  • +
  • -

#9 farrell2k  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 823
  • View blog
  • Posts: 2,536
  • Joined: 29-July 11

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

Posted 29 January 2013 - 09:23 AM

Haha. Nice catch, CasiOo. :)/>

        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

Was This Post Helpful? 0
  • +
  • -

#10 CasiOo  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 1391
  • View blog
  • Posts: 3,077
  • Joined: 05-April 11

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

Posted 29 January 2013 - 09:31 AM

View Postdfl2m, on 29 January 2013 - 04:20 PM, said:

View Postfarrell2k, on 29 January 2013 - 09:48 AM, said:

You have an array of integers and you can't imagine how to substitute its values instead???



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 :bigsmile:
Was This Post Helpful? 0
  • +
  • -

#11 dfl2m  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 05-December 12

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

Posted 29 January 2013 - 09:32 AM

View PostCasiOo, on 29 January 2013 - 10:29 AM, said:

View Postdfl2m, on 29 January 2013 - 04:20 PM, said:

View Postfarrell2k, on 29 January 2013 - 09:48 AM, said:

You have an array of integers and you can't imagine how to substitute its values instead???



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.
Was This Post Helpful? 0
  • +
  • -

#12 dfl2m  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 05-December 12

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

Posted 30 January 2013 - 08:31 AM

Okay it seems i figure it out

	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));
		}


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1