-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDictionary.java
More file actions
42 lines (40 loc) · 1.95 KB
/
Copy pathDictionary.java
File metadata and controls
42 lines (40 loc) · 1.95 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
import java.io.File;
import java.io.FileNotFoundException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;
public class Dictionary {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("Доброго времени суток! Давайте посчитаем количество каждой буквы в Вашем тексте:)");
System.out.print("Введите имя файла с текстом (включая формат .txt): ");
String inputFile = scanner.nextLine();
System.out.print("Введите имя файла, куда сохранить результаты (включая формат .txt): ");
String outputFile = scanner.nextLine();
Map<Character, Integer> charCount = new HashMap<>();
try {
File input = new File(inputFile);
Scanner fileScanner = new Scanner(input);
while (fileScanner.hasNextLine()) {
String line = fileScanner.nextLine();
for (char c : line.toCharArray()) {
if (Character.isLetter(c)) {
charCount.put(c, charCount.getOrDefault(c, 0) + 1);
}
}
}
PrintWriter writer = new PrintWriter(outputFile);
for (char c = 'a'; c <= 'z'; c++) {
writer.println(c + " - " + charCount.getOrDefault(c, 0));
}
for (char c = 'A'; c <= 'Z'; c++) {
writer.println(c + " - " + charCount.getOrDefault(c, 0));
}
writer.close();
System.out.println("Результаты записаны в файл: " + outputFile);
} catch (FileNotFoundException e) {
System.out.println("Файл не найден. Проверьте имя файла и попробуйте снова.");
}
}
}