a. Create a Java program using a NetBeans project containing a single class to p
ID: 3685581 • Letter: A
Question
a. Create a Java program using a NetBeans project containing a single class to produce a table of the occurrences of all letters (A thru Z) and digits (0 thru 9) contained in a user input text.
*Program must have a main method, a method to get user input and a method to get the count of any single character. Other methods may be added as needed.
*Program should regard upper and lower case letters as the same (occurrences of ‘A’ and ‘a’ should be counted together as ‘A’).
*All characters that are not letters nor digits should be ignored.
Sample program run:
Enter text: Use your phone and call me at 1-800-223-343 [Enter]
*Do not use any additional classes in the solution.
Explanation / Answer
AllCharactersCount.java
public class AllCharactersCount {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
java.util.Scanner in = new java.util.Scanner(System.in);
System.out.println("Please enter the input string.. ");
String s = in.nextLine();
int count = 0;
char characters[] = {'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9'};
for(int i=0; i<characters.length; i++){
count = getEachCharacterCount(s, characters[i]);
if(count != 0){
System.out.println("'"+characters[i] +"' "+ count);
}
}
}
public static int getEachCharacterCount(String s, char c1){
int count = 0;
for(int i=0; i<s.length(); i++){
if(Character.toLowerCase(s.charAt(i)) == Character.toLowerCase(c1)){
count++;
}
}
return count;
}
}
Output
Please enter the input string..
Use your phone and call me at 1-800-223-343
'A' 3
'C' 1
'D' 1
'E' 3
'H' 1
'L' 2
'M' 1
'N' 2
'O' 2
'P' 1
'R' 1
'S' 1
'T' 1
'U' 2
'Y' 1
'0' 2
'1' 1
'2' 2
'3' 3
'4' 1
'8' 1
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.