import [Link].
Scanner;
// Imports the Scanner class so the program can read input entered by the user from the
keyboard
/*
This program analyzes a paragraph of text.
It performs:
- Word count
- Character count
- Longest word detection
- Word frequency analysis (without collections)
*/
// This multi-line comment explains the overall purpose and features of the program
public class TextAnalyzer {
// Declares a public class named TextAnalyzer, which contains all the logic for text
analysis
public static void main(String[] args) {
// The main method is the starting point of the program execution
Scanner sc = new Scanner([Link]);
// Creates a Scanner object named sc that reads user input from standard input
(keyboard)
[Link]("===== TEXT ANALYZER =====");
// Displays a title so the user knows the program has started
[Link]("Enter a paragraph of text:");
// Prompts the user to enter a paragraph that will be analyzed
String text = [Link]().toLowerCase().trim();
// Reads the entire line of text entered by the user
// Converts all characters to lowercase to ensure consistent comparisons
// Removes any leading or trailing spaces to avoid counting empty words
if ([Link]() == 0) {
// Checks if the user entered an empty string after trimming whitespace
[Link]("No text entered.");
// Informs the user that no valid text was provided
return;
// Stops program execution since there is no text to analyze
}
int charCount = [Link]();
// Stores the total number of characters in the text, including spaces
String[] words = [Link]("\\s+");
// Splits the text into individual words using one or more spaces as a delimiter
// Stores the resulting words in an array
int wordCount = [Link];
// Stores the total number of words by checking the array length
String longestWord = words[0];
// Initializes the longestWord variable with the first word in the array
// This provides a starting point for comparison
for (String word : words) {
// Uses an enhanced for-loop to go through each word in the words array
if ([Link]() > [Link]()) {
// Compares the length of the current word with the length of the longest word
found so far
longestWord = word;
// Updates longestWord if the current word is longer
}
}
boolean[] counted = new boolean[[Link]];
// Creates a boolean array to track which words have already been counted
// This prevents duplicate frequency counts for the same word
[Link]("\nWord Frequencies:");
// Prints a heading for the word frequency output section
for (int i = 0; i < [Link]; i++) {
// Loops through each word in the words array by index
if (counted[i]) continue;
// Skips this iteration if the word at index i has already been counted
int count = 1;
// Initializes the frequency count for the current word
for (int j = i + 1; j < [Link]; j++) {
// Inner loop that compares the current word with the remaining words in the
array
if (words[i].equals(words[j])) {
// Checks if the current word matches another word in the array by using
.equals
count++;
// Increments the frequency count when a match is found
counted[j] = true;
// Marks the matching word as counted to avoid recounting it
}
}
[Link](words[i] + " -> " + count);
// Displays the word and the number of times it appears in the text
}
[Link]("\n===== STATISTICS =====");
// Prints a heading for the summary statistics section
[Link]("Characters : " + charCount);
// Displays the total number of characters in the text
[Link]("Words : " + wordCount);
// Displays the total number of words in the text
[Link]("Longest : " + longestWord);
// Displays the longest word found in the text
[Link]("\nText analysis complete.");
// Displays a message indicating the program has finished running
}
}