Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
15 views

Programming Assignment Unit 1

Uploaded by

fmisomali
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views

Programming Assignment Unit 1

Uploaded by

fmisomali
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

Programming Assignment Unit 1

import java.util.Scanner;
import java.util.HashMap;
import java.util.InputMismatchException;
import java.util.Map;
import java.util.Set;
import java.util.HashSet;

/**
* The `TextAnalyzer` class provides methods for analyzing and processing text input.
* It includes functionality related to character and word analysis.
* Users can input paragraphs or lengthy text for analysis.
* @author Foster Misomali
*/

public class TextAnalyzer {


// Method to find the most common character
private static char mostCommonCharacter(String text) {
Map<Character, Integer> charFrequencyMap = new HashMap<>();
for (char c : text.toCharArray()) {
charFrequencyMap.put(c, charFrequencyMap.getOrDefault(c, 0) + 1);
}
return charFrequencyMap.entrySet().stream()
.max(Map.Entry.comparingByValue())
.map(Map.Entry::getKey)
.orElse('\0'); // Return null character if no valid character found
}
// Method to count character frequency
private static int countCharacterFrequency(String text, char targetChar) {
int frequency = 0;
for (char c : text.toCharArray()) {
if (Character.toLowerCase(c) == targetChar) {
frequency++;
}
}
return frequency;
}

// Method to count word frequency


public static int countWordFrequency(String[] words, String targetWord) {
int frequency = 0;
for (String word : words) {
if (word.toLowerCase().equals(targetWord)) {
frequency++;
}
}
return frequency;
}

// Method to count unique words


private static int countUniqueWords(String[] words) {
Set<String> uniqueWords = new HashSet<>();
for (String word : words) {
uniqueWords.add(word.toLowerCase());
}
return uniqueWords.size();
}
public static void main(String[] args) {
try (Scanner scanner = new Scanner(System.in)) {
System.out.print("Enter a paragraph or lengthy text: ");
String inputText = scanner.nextLine();

int choice = 0;
do {
System.out.println("1. Total Characters");
System.out.println("2. Total Words");
System.out.println("3. Most Common Character");
System.out.println("4. Check Frequency of Character");
System.out.println("5. Check Frequency of Word");
System.out.println("6. Check Unique Word");
System.out.println("7. Exit");
System.out.print("Enter your choice: ");

try {
choice = scanner.nextInt();
scanner.nextLine(); // Consume newline

switch (choice) {
case 1:
// Find total characters
System.out.println("Number of total characters is: " +
inputText.length());
break;

case 2:
//Find total words
String[] words = inputText.split("\\s+");
System.out.println("Number of total words is: " + words.length);
break;

case 3:
//Find most common character
char mostCommonChar = mostCommonCharacter(inputText);
System.out.println("Most common character is: " +
mostCommonChar);
break;

case 4:
//Check character frequency
char targetChar;
do {
System.out.print("Enter a single character to check its frequency: ");
String userInput = scanner.nextLine();
if (userInput.length() == 1 && Character.isLetter(userInput.charAt(0))) {
targetChar = userInput.charAt(0);
break; // Valid input, exit loop

} else {
System.out.println("Please enter a valid single character.");
}
} while (true);
int charFrequency = countCharacterFrequency(inputText, targetChar);
System.out.println("Frequency of '" + targetChar + "' is: " + charFrequency);
break;

case 5:
//Check word frequency
System.out.print("Enter a word to check its frequency: ");
String[] word = inputText.split("\\s+");
String targetWord = scanner.nextLine().toLowerCase();

// Validate user input (word must contain only letters)


while (!targetWord.matches("[a-zA-Z]+")) {
System.out.print("Invalid input. Please enter a valid
word: ");
targetWord = scanner.nextLine().toLowerCase();
}

int wordFrequency = countWordFrequency(word,


targetWord);
System.out.println("Frequency of word '" + targetWord + "'
is: " + wordFrequency);
break;

case 6:
//Find unique words
String[] wordArray = inputText.split("\\s+");
int uniqueWordCount = countUniqueWords(wordArray);
System.out.println("Number of unique word is: " +
uniqueWordCount);
break;

case 7:
//Exit the program
System.out.println("Exiting. Have a great day!");
break;

default:
System.out.println("Invalid choice. Please select a valid
option.");
}
} catch (InputMismatchException e) {
System.out.println("Error! Please enter a valid number choice.");
scanner.nextLine(); // Consume newline
}
} while (choice != 7);
}
}
}

You might also like