Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package StreamsFilesAndDirectories4;
- import java.io.BufferedWriter;
- import java.io.FileWriter;
- import java.io.IOException;
- import java.nio.file.Files;
- import java.nio.file.Path;
- import java.util.HashSet;
- import java.util.List;
- import java.util.Set;
- public class CountCharacterTypes {
- public static void main(String[] args) throws IOException {
- int vowelsCount = 0;
- int punctuationCount = 0;
- int consonantsCount = 0;
- Set<Character> vowels = getVowels();
- Set<Character> punctuationalMarks = getPuntMarks();
- String path = "C:\\Users\\User\\Desktop\\Java Advanced - 10.01.23 - 13.09.22\\src\\StreamsFilesAndDirectories4\\04. Java-Advanced-Files-and-Streams-Exercises-Resources\\input.txt";
- List<String> allLines = Files.readAllLines(Path.of(path));
- for (String line : allLines) {
- for (int index = 0; index < line.length(); index++) {
- char currentSymbol = line.charAt(index);
- if (currentSymbol == ' ') {
- continue;
- }
- if (vowels.contains(currentSymbol)) {
- vowelsCount++;
- } else if (punctuationalMarks.contains(currentSymbol)) {
- punctuationCount++;
- } else {
- consonantsCount++;
- }
- }
- }
- BufferedWriter writer = new BufferedWriter(new FileWriter("output_4.txt"));
- writer.write("Vowels: " + vowelsCount);
- writer.newLine();
- writer.write("Other symbols: " + consonantsCount);
- writer.newLine();
- writer.write("Punctuation: " + punctuationCount);
- writer.close();
- }
- private static Set<Character> getPuntMarks() {
- Set<Character> marks = new HashSet<>();
- marks.add('!');
- marks.add('?');
- marks.add('.');
- marks.add(',');
- return marks;
- }
- private static Set<Character> getVowels() {
- Set<Character> vowels = new HashSet<>();
- vowels.add('a');
- vowels.add('e');
- vowels.add('i');
- vowels.add('o');
- vowels.add('u');
- return vowels;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement