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]);
}
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.