1

Recently I was asked to work on an assignment that is to write a simple Java app that reads a telephone number as a string and prints out the frequency of each digit in the telephone number. However, after looking at this carefully with my partner, I came under some confusion as to why I need the second loop lines, codes are as following

public class CharacterFrequency {

public static Scanner kbd = new Scanner(System.in);

public static int MAXSIZE=10; //Constant for array size and easy change

public static void main(String[] args) {

    int telephoneNumArrayIndex = 0; //index where array will start checking
    char [] telephoneNumArray = new char[MAXSIZE]; //array holding tel Number digits.

    String telephoneNumber;//string that will that will read input from user.

    System.out.print("Please Enter a 10-digit Telephone Number: ");
    telephoneNumber = kbd.next();

    System.out.println("\nThe Following is the Number of Times Each Digit Appears:\n");

    //loop that will check and test  array for digits and ignore "white space" 
    //characters (-,*,., ,etc)
    for (int i = 0; i < telephoneNumber.length(); i++) {
        if (telephoneNumber.charAt(i) >= (char) 48
                && telephoneNumber.charAt(i) <= (char) 57) {
            telephoneNumArray[telephoneNumArrayIndex++] = telephoneNumber.charAt(i);
        }
    }

    //reasoning behind the loop. ??????
    int[] counter = new int[MAXSIZE];
    //loop to fill 
    for (int i = 0; i < counter.length; i++) {
        counter[i] = 0;
        System.out.println(counter[i]);
    }

    //loop that stores the frequency of each digit 0-9 to its corresponding array 
    //index. the char is then parsed to convert to int datatype in order to use the counter
    //in the array.
    for (int i = 0; i < telephoneNumArray.length; i++) {
        for (int j = 0; j < 10; j++) {
            if (telephoneNumArray[i] == (char) (j + 48)) {
                counter[j]++;
            }
        }
    }

    //loop that will display the frequency (counter[i]) of each digit (i),
    //used in a typical U.S. phone number by looping through each index of the array
    //and printing the number corresponding to that count from 0-9 

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


}

}

The result is the same either way, but was wondering if perhaps having it is more efficient ?

Ratul Sharker
  • 7,484
  • 4
  • 35
  • 44
mlopman
  • 67
  • 1
  • 1
  • 7

2 Answers2

0

The second loop's purpose is to initialise all the values in the counter array to 0. However in Java, an array of primitives has each element initialised to its default value. An int's default value is 0, so the counter array already has everything set to 0. So that loop is pointless and can be removed.

tixopi
  • 486
  • 3
  • 14
0

The loop in question exists to initialize the values in the count array. An uninitialized variable in many languages is null, and if you try to calculate increment it (I.E. the operation null +1) you will get an exception because this isn't a supported operation.

This is a valid concern and a reason to have such a loop (or other initialization procedure) in many languages. However in Java it isn't needed. In Java simple types cannot be null. This means that if you don't initliaze them they go to a default value (0 for ints & shorts).

See this question for a more in-depth analysis of the issue.

Community
  • 1
  • 1
Knells
  • 827
  • 2
  • 12
  • 24