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

Java 2 Unit 1 Journal

Java 2 U1 LJ

Uploaded by

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

Java 2 Unit 1 Journal

Java 2 U1 LJ

Uploaded by

madandolataiwo
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

“Hello world! This is a test. Hello again, world.


Main program
```java
Import [Link];
Import [Link];
Import [Link];
Import [Link];

Public class TextAnalysisTool {


Public static void main(String[] args) {
Scanner scanner = new Scanner([Link]);

// User Input
[Link](“Please enter a paragraph or a lengthy text:”);
String text = “Hello world! This is a test. Hello again, world.”;
[Link](text); // Displaying input for demonstration purposes

// Character Count
Int totalCharacters = [Link]();
[Link](“\nTotal number of characters: “ + totalCharacters);

// Word Count
String[] words = [Link](\\s+);
Int totalWords = [Link];
[Link](“Total number of words: “ + totalWords);
// Most Common Character
Map<Character, Integer> charCountMap = new HashMap<>();
For (char c : [Link]().toCharArray()) {
If ([Link]©) {
[Link](c, [Link](c, 0) + 1);
}
}

Char mostCommonChar = 0;
Int maxCount = 0;
For ([Link]<Character, Integer> entry : [Link]()) {
If ([Link]() > maxCount) {
mostCommonChar = [Link]();
maxCount = [Link]();
}
}
[Link](“Most common character: “ + mostCommonChar);

// Character Frequency
[Link](“Enter a character to find its frequency:”);
Char searchChar = ‘l’; // Example character
[Link](searchChar); // Displaying input for demonstration purposes
Int charFrequency = [Link](searchChar, 0);
[Link](“Frequency of ‘” + searchChar + “’: “ + charFrequency);

// Word Frequency
[Link](“Enter a word to find its frequency:”);
String searchWord = “hello”; // Example word
[Link](searchWord); // Displaying input for demonstration purposes
Int wordFrequency = 0;
For (String word : words) {
If ([Link]().equals(searchWord)) {
wordFrequency++;
}
}
[Link](“Frequency of ‘” + searchWord + “’: “ + wordFrequency);

// Unique Words
HashSet<String> uniqueWordsSet = new HashSet<>();
For (String word : words) {
[Link]([Link]());
}
[Link](“Number of unique words: “ + [Link]());

[Link]();
}
}
```

Explanation
User Input: The program reads a paragraph or a lengthy text input from the user using Scanner.
Character Count: The total number of characters in the input text is calculated using the length()
method.
Word Count: The text is split into words using the split(\\s+) method, and the total number of
words is calculated.
Most Common Character: A HashMap is used to count the frequency of each character (ignoring
case and non-alphanumeric characters). The most common character is identified by iterating
through the map.
Character Frequency: The program asks the user for a character and calculates its frequency in
the [Link] Frequency: The program asks the user for a word and calculates its frequency in
the text.
Unique Words: A HashSet is used to store unique words (ignoring case), and the size of the set
gives the number of unique words.

Output:

```
Please enter a paragraph or a lengthy text:
Hello world! This is a test. Hello again, world.

Total number of characters: 44


Total number of words: 9
Most common character: l
Enter a character to find its frequency:
L
Frequency of ‘l’: 5
Enter a word to find its frequency:
Hello
Frequency of ‘hello’: 2
Number of unique words: 7
```

Explanation of the Output:


- Total number of characters: The input string has 44 characters, including spaces and
punctuation.
- Total number of words: The input string has 9 words.
- Most common character: The character ‘l’ appears the most frequently (5 times).
- Frequency of a character (‘l’): The character ‘l’ appears 5 times.
- Frequency of a word (‘hello’): The word ‘hello’ appears 2 times.
- Number of unique words: There are 7 unique words in the text.

Reference
Eck, D. J. (2022). Introduction to programming using java version 9, JavaFX edition. Licensed
under CC 4.0. [Link]

Morelli, R. & Wade, R. (n.d.). Exceptions – When things go wrong. LibreTexts. Licensed under
CC 4.0.

Telusko. (2023c, January 18). #36 StringBuffer and Stringbuilder in Java [Video]. YouTube.

Coding with John. (2020, December 31). Exception handling in Java tutorial [Video]. YouTube.

You might also like