Programming Assignment Unit 1
Timothy J Reid
University of the People
CS 1103: Programming 2
Alejandro Lara
June 25, 2024
1
Introduction
In this assignment, I will create a text analysis tool that performs various operations on a
given text input. This tool will help users gain insights into their text data by performing
character and word analysis. The operations include calculating the total number of characters
and words, finding the most common character, determining character and word frequency, and
identifying the number of unique words.
Explanation of Code
1. User Input: The program begins by prompting the user to input a paragraph or a lengthy text.
The input is validated to ensure it is not empty. If the user enters an empty text, the program will
prompt the user again until a valid input is provided.
2. Character Count: The program calculates the total number of characters in the input text
using the `length()` method of the `String` class and displays the result (Eck, 2022).
3. Word Count: The input text is split into words using the `split("\\s+")` method, which splits
the text based on whitespace. The total number of words is then calculated and displayed (Eck,
2022).
2
4. Most Common Character: The program uses a `HashMap` to count the frequency of each
character in the text, converting the text to lowercase and ignoring non-alphanumeric characters.
The most common character is then determined and displayed.
5. Character Frequency: The user is asked to input a character. The program calculates the
frequency of occurrences of this character in the text, ensuring case-insensitivity by converting
both the text and the character to lowercase, and displays the result (Morelli & Wade, n.d.).
6. Word Frequency: The user is asked to input a word. The program calculates the frequency of
occurrences of this word in the text, ensuring case-insensitivity, and displays the result (Morelli
& Wade, n.d.).
7. Unique Words: The program uses a `HashSet` to store unique words from the text, ensuring
case-insensitivity by converting the text to lowercase. The count of unique words is then
displayed (Samoylov, 2018).
3
Conclusion
This text analysis tool demonstrates the basics of string handling and text analysis in Java. By
implementing character and word counts, frequency analysis, and identifying unique words, I have
reinforced my understanding of string manipulation and data analysis techniques. Now, I can see how
tools like this are commonly used in word processors to provide features like word count and character
count, and in social media analytics to analyze posts and trends. This hands-on experience has shown me
the practical applications of Java programming concepts and how they are used to create useful utilities
for text data analysis. Additionally, the program includes input validation to handle cases where the user
might not enter any data, ensuring it runs smoothly without errors.
4
Reference
Eck, D. J. (2022). Introduction to Programming Using Java, Version 9, JavaFX Edition. Hobart
and William Smith Colleges. Available at
https://math.hws.edu/javanotes/](https://math.hws.edu/javanotes/
Morelli, R., & Wade, R. (n.d.). Exceptions - When Things Go Wrong. LibreTexts. Licensed
under CC 4.0.
Samoylov, N. (2018). Introduction to Programming: Learn to Program in Java with Data
Structures, Algorithms, and Logic. Packt Publishing, Limited.
5
Java Code:
import java.util.*;
public class TextAnalysisTool {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
// User Input: Ask the user to input a paragraph or a lengthy text
String inputText = "";
while (inputText.trim().isEmpty()) {
System.out.println("Please enter a paragraph or a lengthy text:");
inputText = scanner.nextLine();
if (inputText.trim().isEmpty()) {
System.out.println("Input cannot be empty. Please enter some
text.");
}
}
// Character Count
int characterCount = inputText.length();
System.out.println("Total number of characters: " + characterCount);
// Word Count
String[] words = inputText.split("\\s+");
int wordCount = words.length;
System.out.println("Total number of words: " + wordCount);
// Most Common Character
char mostCommonCharacter = getMostCommonCharacter(inputText);
System.out.println("Most common character: " + mostCommonCharacter);
// Character Frequency
System.out.println("Enter a character to find its frequency:");
String charInput = scanner.nextLine().toLowerCase();
if (charInput.isEmpty()) {
System.out.println("No character entered.");
} else {
char characterToCheck = charInput.charAt(0);
int charFrequency = getCharacterFrequency(inputText,
characterToCheck);
System.out.println("Frequency of '" + characterToCheck + "': " +
charFrequency);
}
6
// Word Frequency
System.out.println("Enter a word to find its frequency:");
String wordToCheck = scanner.nextLine().toLowerCase();
int wordFrequency = getWordFrequency(inputText, wordToCheck);
System.out.println("Frequency of \"" + wordToCheck + "\": " +
wordFrequency);
// Unique Words
int uniqueWordCount = getUniqueWordCount(inputText);
System.out.println("Number of unique words: " + uniqueWordCount);
scanner.close();
}
private static char getMostCommonCharacter(String text) {
Map<Character, Integer> charCountMap = new HashMap<>();
for (char c : text.toLowerCase().toCharArray()) {
if (Character.isLetterOrDigit(c)) {
charCountMap.put(c, charCountMap.getOrDefault(c, 0) + 1);
}
}
return Collections.max(charCountMap.entrySet(),
Map.Entry.comparingByValue()).getKey();
}
private static int getCharacterFrequency(String text, char character) {
int frequency = 0;
for (char c : text.toLowerCase().toCharArray()) {
if (c == character) {
frequency++;
}
}
return frequency;
}
private static int getWordFrequency(String text, String word) {
String[] words = text.toLowerCase().split("\\s+");
int frequency = 0;
for (String w : words) {
if (w.equals(word)) {
frequency++;
}
}
return frequency;
7
private static int getUniqueWordCount(String text) {
Set<String> uniqueWords = new
HashSet<>(Arrays.asList(text.toLowerCase().split("\\s+")));
return uniqueWords.size();
}
}
Output without errors:
Output with input validation error: