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

Java Text Analysis Program

The Java program 'TextAnalyzer' analyzes a user-provided paragraph by counting characters and words, detecting the longest word, and performing word frequency analysis. It uses a Scanner to read input, processes the text to gather statistics, and outputs the results. The program handles empty input gracefully and provides a summary of the analysis upon completion.

Uploaded by

msh.tces
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)
6 views3 pages

Java Text Analysis Program

The Java program 'TextAnalyzer' analyzes a user-provided paragraph by counting characters and words, detecting the longest word, and performing word frequency analysis. It uses a Scanner to read input, processes the text to gather statistics, and outputs the results. The program handles empty input gracefully and provides a summary of the analysis upon completion.

Uploaded by

msh.tces
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

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
}
}

You might also like