0% found this document useful (0 votes)
2 views3 pages

unit 1 java

The TextAnalysisTool is a Java program that analyzes user-provided text for various metrics. It calculates the total number of characters, words, the most common character, and the frequency of a specified character and word. Additionally, it counts the number of unique words in the text.

Uploaded by

belovedvince
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views3 pages

unit 1 java

The TextAnalysisTool is a Java program that analyzes user-provided text for various metrics. It calculates the total number of characters, words, the most common character, and the frequency of a specified character and word. Additionally, it counts the number of unique words in the text.

Uploaded by

belovedvince
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

import java.util.

*;

public class TextAnalysisTool {

public static void main(String[] args) {


Scanner scanner = new Scanner(System.in);

// User Input: Get paragraph


System.out.println("Enter a paragraph or lengthy text:");
String text = scanner.nextLine().trim();

while (text.isEmpty()) {
System.out.println("Text cannot be empty. Please enter again:");
text = scanner.nextLine().trim();
}

// Character Count
int charCount = text.length();
System.out.println("\nTotal number of characters: " + charCount);

// Word Count
String[] words = text.trim().split("\\s+");
int wordCount = words.length;
System.out.println("Total number of words: " + wordCount);

// Most Common Character (excluding spaces, case-insensitive)


Map<Character, Integer> charFrequency = new HashMap<>();
for (char ch : text.toLowerCase().toCharArray()) {
if (ch != ' ') {
charFrequency.put(ch, charFrequency.getOrDefault(ch, 0) + 1);
}
}

char mostCommonChar = ' ';


int maxFreq = 0;
for (Map.Entry<Character, Integer> entry : charFrequency.entrySet()) {
if (entry.getValue() > maxFreq) {
maxFreq = entry.getValue();
mostCommonChar = entry.getKey();
}
}

System.out.println("Most common character: " + mostCommonChar);

// Character Frequency
System.out.print("\nEnter a character to find its frequency: ");
String charInput = scanner.nextLine().trim().toLowerCase();

while (charInput.isEmpty() || charInput.length() != 1 ||


!Character.isLetterOrDigit(charInput.charAt(0))) {
System.out.print("Please enter a single valid character: ");
charInput = scanner.nextLine().trim().toLowerCase();
}

char targetChar = charInput.charAt(0);


long charFreq = text.toLowerCase().chars().filter(c -> c == targetChar).count();
System.out.println("Frequency of character '" + targetChar + "': " + charFreq);

// Word Frequency
System.out.print("\nEnter a word to find its frequency: ");
String wordInput = scanner.nextLine().trim().toLowerCase();

while (wordInput.isEmpty()) {
System.out.print("Please enter a valid word: ");
wordInput = scanner.nextLine().trim().toLowerCase();
}

int wordFreq = 0;
for (String word : words) {
if (word.toLowerCase().equals(wordInput)) {
wordFreq++;
}
}
System.out.println("Frequency of word \"" + wordInput + "\": " + wordFreq);

// Unique Words
Set<String> uniqueWords = new HashSet<>();
for (String word : words) {
uniqueWords.add(word.toLowerCase());
}
System.out.println("Number of unique words: " + uniqueWords.size());

scanner.close();
}
}

You might also like