Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

import java.util.*; public class CharacterFrequency { public static Scanner kbd;

ID: 3699860 • Letter: I

Question

import java.util.*;

public class CharacterFrequency {
  
   public static Scanner kbd;
  
   public static void main(String[] args) {

       kbd = new Scanner(System.in);
      
      
      
       kbd.close();
   }
}

(Here is the starter code and please use it)

Write a program that counts the number of times a digit appears in a telephone number.

Your program should begin by creating an array of size 10 that will hold the count for each digit from 0 to 9. Next, read a telephone number from the keyboard. The telephone number should be stored in a String variable. Examine each character in the phone number, and increment the appropriate count in the array. Display the contents of the array.

Note, the user might enter only numbers, as in: 2525551212
or they might enter dashes between the digits, as in: 252-555-1212
or they might enter parentheses and dashes, as in: (252)555-1212
or they might enter parentheses, dashes and spaces, as in: (252) 555-1212

Your program should be able to handle all of these possibilities.

Hint: Ignore any character that isn't a digit.
Hint: Use one of the Character methods to determine if a character is a digit or not.

The following is an example of what your MIGHT see on the screen when your program runs. The exact output depends on what values that the user types in while the program runs. The user's values are shown below in italics:

Please enter a 10-digit telephone number: 252-555-1212

The following is the number of times each digit appears:
0 - 0
1 - 2
2 - 4
3 - 0
4 - 0
5 - 4
6 - 0
7 - 0
8 - 0
9 - 0

Program cannot use 'break' statements to exit loops

And please include comments

Program must use arrays

Explanation / Answer

CharacterFrequency.java

import java.util.*;

public class CharacterFrequency {

  

public static Scanner kbd;

  

public static void main(String[] args) {

kbd = new Scanner(System.in);

  

System.out.println("Please enter a 10-digit telephone number: ");

String phone = kbd.nextLine();

int numbers[] = new int[10];

for(int i=0;i<phone.length();i++) {

if(Character.isDigit(phone.charAt(i))) {

numbers[phone.charAt(i)-'0']++;

}

}

for(int i=0;i<numbers.length;i++) {

System.out.println(i+" - "+numbers[i]);

}

  

kbd.close();

}

}

Output:

Please enter a 10-digit telephone number:
252-555-1212
0 - 0
1 - 2
2 - 4
3 - 0
4 - 0
5 - 4
6 - 0
7 - 0
8 - 0
9 - 0