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

Write a program in class CharacterFrequency that counts the number of times a di

ID: 3545325 • Letter: W

Question

Write a program in class CharacterFrequency that counts the number of times a digit appears in a telephone number. The program should create an array of size 10 that will hold the count for each digit from 0 to 9. Read a telephone number from the keyboard as a string. Examine each character in the phone number and increment the appropriately count in the array. Display the contents of the array.

Write a program in class CharacterFrequency that counts the number of times a digit appears in a telephone number. The program should create an array of size 10 that will hold the count for each digit from 0 to 9. Read a telephone number from the keyboard as a string. Examine each character in the phone number and increment the appropriately count in the array. Display the contents of the array.

Explanation / Answer

public class CountDemNumba {

public static void main(String[] args) {
int[] demCount = new int[10];
java.util.Scanner sc = new java.util.Scanner(System.in);
System.out.print("Gimme yor noomba ==> ");
String datPhoneNum = sc.nextLine();
String cleanDat = datPhoneNum.replaceAll("[^0-9]", "");
for (int i = 0; i < cleanDat.length(); i++) {
int cNo = cleanDat.charAt(i)-'0';
demCount[ cNo ]++;
}
for (int i = 0; i < demCount.length; i++) {
if( demCount[i] != 0 ) {
System.out.printf("Nomba 'of dem %d's: %d%n", i, demCount[i]);
}

}
}
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote