Programming 2
Assignment unit1
import java.util.HashMap;
import java.util.Scanner;
public class StringHandling {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
// user input
System.out.println("Enter a paragraph or lengthy text:");
String inputText = scanner.nextLine();
// character count
int characterCount = inputText.length();
System.out.println("Total number of characters: " +
characterCount);
// word count
String[] words = inputText.trim().split("\\s+");
int wordCount = words.length;
System.out.println("Total number of words: " + wordCount);
// most common character
HashMap<Character, Integer> charMap = new HashMap<>();
for (char c : inputText.toCharArray()) {
charMap.put(c, charMap.getOrDefault(c, 0) + 1);
}
char mostCommonChar = ' ';
int maxCount = 0;
for (char c : charMap.keySet()) {
if (charMap.get(c) > maxCount) {
maxCount = charMap.get(c);
mostCommonChar = c;
}
}
System.out.println("Most common character: " + mostCommonChar);
// character frequency
System.out.println("Enter a character:");
char targetChar =
Character.toLowerCase(scanner.next().charAt(0));
int charFrequency = 0;
for (char c : inputText.toLowerCase().toCharArray()) {
if (c == targetChar) {
charFrequency++;
}
}
System.out.println("Frequency of '" + targetChar + "': " +
charFrequency);
// word frequency
System.out.println("Enter a word to check its frequency:");
String targetWord = scanner.next().toLowerCase();
int wordFrequency = 0;
for (String word : words) {
if (word.toLowerCase().equals(targetWord)) {
wordFrequency++;
}
}
System.out.println("Frequency of '" + targetWord + "': " +
wordFrequency);
// unique words
HashMap<String, Integer> wordMap = new HashMap<>();
for (String word : words) {
wordMap.put(word.toLowerCase(),
wordMap.getOrDefault(word.toLowerCase(), 0) + 1);
}
int uniqueWordCount = wordMap.size();
System.out.println("Total number of unique words: " +
uniqueWordCount);
scanner.close();
}
}
Explanations
User input: The program prompts the user to enter text and stores it in the
text variable.
Character count: it calculates the length of the text string and displays the
character count.
Word count: it uses split to split the text into words based on spaces and
counts the number of elements in the resulting array.
Most common character: HashMap is used to store character frequencies.
The character with the highest count is identified and displayed.
Character frequency: The user enters a character, its lowercase version is
compared against each character in the lowercase text, and the frequency is
displayed.
Word frequency: The user enters a word, its lowercase version is compared
against each lowercase word in the text, and the frequency is displayed.
Unique words: Another HashMap is used to store unique words(lowercase)
and their occurrence count. The size of this map represents the number of
unique words.
Reference
ECK, D. J. (2022). Introduction to programming using java version 9, JavaFX
edition. Licensed und e CC.4.0. Use the introduction to programming using
java for pdf version of the file