-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTextAnalyzer.java
More file actions
142 lines (124 loc) · 5.47 KB
/
Copy pathTextAnalyzer.java
File metadata and controls
142 lines (124 loc) · 5.47 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
import java.io.*;
import java.util.Scanner;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
public class TextAnalyzer {
private static Scanner scanner = new Scanner(System.in);
public static void main(String[] args) {
while (true) {
displayMenu();
int choice = getUserChoice();
switch (choice) {
case 1:
analyzeFromInput();
break;
case 2:
analyzeFromFile();
break;
case 3:
displayHelp();
break;
case 4:
System.out.println("\nGoodbye!");
scanner.close();
return;
default:
System.out.println("\nInvalid choice. Please try again.");
}
}
}
private static void displayMenu() {
System.out.println("\n=== Text Analyzer ===");
System.out.println("1. Analyze text from keyboard input");
System.out.println("2. Analyze text from file");
System.out.println("3. Help");
System.out.println("4. Exit");
System.out.print("Enter your choice (1-4): ");
}
private static int getUserChoice() {
try {
return Integer.parseInt(scanner.nextLine());
} catch (NumberFormatException e) {
return -1;
}
}
private static void analyzeFromInput() {
System.out.println("\nEnter your text (type 'END' on a new line when finished):");
StringBuilder text = new StringBuilder();
String line;
while (!(line = scanner.nextLine()).equals("END")) {
text.append(line).append("\n");
}
analyzeText(text.toString());
}
private static void analyzeFromFile() {
System.out.print("\nEnter the file path: ");
String filePath = scanner.nextLine();
try {
String content = new String(java.nio.file.Files.readAllBytes(
java.nio.file.Paths.get(filePath)));
analyzeText(content);
} catch (IOException e) {
System.out.println("Error reading file: " + e.getMessage());
}
}
private static void analyzeText(String text) {
// Basic statistics
int charCount = text.length();
int charNoSpaces = text.replaceAll("\\s+", "").length();
String[] words = text.trim().split("\\s+");
int wordCount = text.trim().isEmpty() ? 0 : words.length;
int sentenceCount = text.split("[.!?]+").length;
// Average word length
double avgWordLength = wordCount > 0 ?
(double) charNoSpaces / wordCount : 0;
// Display results
System.out.println("\n=== Analysis Results ===");
System.out.println("Total characters: " + charCount);
System.out.println("Characters (no spaces): " + charNoSpaces);
System.out.println("Word count: " + wordCount);
System.out.println("Sentence count: " + sentenceCount);
System.out.printf("Average word length: %.2f characters\n", avgWordLength);
// Ask to save results
System.out.print("\nWould you like to save these results? (y/n): ");
if (scanner.nextLine().trim().equalsIgnoreCase("y")) {
saveResults(text, charCount, charNoSpaces, wordCount,
sentenceCount, avgWordLength);
}
}
private static void saveResults(String text, int charCount, int charNoSpaces,
int wordCount, int sentenceCount, double avgWordLength) {
String timestamp = LocalDateTime.now().format(
DateTimeFormatter.ofPattern("yyyyMMdd_HHmmss"));
String filename = "analysis_" + timestamp + ".txt";
try (PrintWriter writer = new PrintWriter(filename)) {
writer.println("=== Text Analysis Report ===");
writer.println("Generated: " + LocalDateTime.now());
writer.println("\n=== Input Text ===");
writer.println(text);
writer.println("\n=== Statistics ===");
writer.println("Total characters: " + charCount);
writer.println("Characters (no spaces): " + charNoSpaces);
writer.println("Word count: " + wordCount);
writer.println("Sentence count: " + sentenceCount);
writer.printf("Average word length: %.2f characters\n", avgWordLength);
System.out.println("Results saved to: " + filename);
} catch (IOException e) {
System.out.println("Error saving results: " + e.getMessage());
}
}
private static void displayHelp() {
System.out.println("\n=== Help ===");
System.out.println("1. Analyze text from keyboard input:");
System.out.println(" - Type or paste your text");
System.out.println(" - Type 'END' on a new line when finished");
System.out.println(" - View the analysis results");
System.out.println(" - Optionally save results to a file");
System.out.println("\n2. Analyze text from file:");
System.out.println(" - Enter the path to your text file");
System.out.println(" - View the analysis results");
System.out.println(" - Optionally save results to a file");
System.out.println("\nPress Enter to continue...");
scanner.nextLine();
}
}