. Create a Java program using a NetBeans project containing a single class to pr
ID: 3685762 • Letter: #
Question
. 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 (called a histogram). Use the two methods created during Lab 08 and add any additional methods. Also, assemble a diagram to describe the structure of the methods in your solution. See Lecture 08 or Figure 6.11 in the textbook for an example. Regardless of how the diagram is created, export it as a PDF file in preparation for upload. i. 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. ii. Program should regard upper and lower case letters as the same (occurrences of ‘A’ and ‘a’ should be counted together as ‘A’). iii. All characters that are not letters nor digits should be ignored. iv. Sample program run: Enter text: Use your phone and call me at 1-800-223-343 [Enter] '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': 1Explanation / Answer
/*CountingOccurences.java to count occurence of a character and a number int a text */
import java.util.Scanner;
public class CountingOccurences {
public static String input()
{
Scanner inp= new Scanner(System.in);
String str;
System.out.println("Enter text: ");
str = inp.nextLine(); // take input
str = str.toUpperCase(); // convert to uppercase
return str;
}
public static int countcharacter(String haystack, char needle)
{
int count = 0;
for (int i=0; i < haystack.length(); i++)
{
if (haystack.charAt(i) == needle)
{
count++; // count occurence of character
}
}
return count;
}
public static void main(String[] args) {
String str = input();
char ch;
int count ;
for (char c = 'A'; c <= 'Z' ;c++ )
{
count = countcharacter(str,c);
if(count > 0)
System.out.println(c + ": " + count);
}
for (char c = '0' ; c <= '9' ;c++ )
{
count = countcharacter(str,c);
if(count > 0)
System.out.println(c + ": " + count);
}
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.