Advertisement
desislava_topuzakova

4. Count Character Types

Jan 22nd, 2022
703
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.82 KB | None | 0 0
  1. package StreamsFilesDirectories_Exercise;
  2.  
  3. import java.io.BufferedWriter;
  4. import java.io.FileWriter;
  5. import java.io.IOException;
  6. import java.nio.file.Files;
  7. import java.nio.file.Path;
  8. import java.util.HashSet;
  9. import java.util.List;
  10. import java.util.Set;
  11.  
  12. public class CountCharacterTypes_04 {
  13.     public static void main(String[] args) throws IOException {
  14.         //гласни: a, e, i, o, u
  15.         //пунктуационни знаци: ! , . ?
  16.         //съгласни: всичко останало
  17.         int vowelsCount = 0; //броя на гласните букви
  18.         int punctCount = 0; //броя на пунктуационните знаци
  19.         int consonantsCount = 0; //броя на съгласните букви
  20.  
  21.         Set<Character> vowels = getVowels(); //всички възможни главни букви
  22.         Set<Character> punctuationalMarks = getPuntMarks(); //всички възможни пунктуационни знаци
  23.  
  24.         String path = "C:\\Users\\I353529\\Desktop\\04. Java-Advanced-Files-and-Streams-Exercises-Resources\\input.txt";
  25.         List<String> allLines = Files.readAllLines(Path.of(path));
  26.  
  27.         for (String line : allLines) {
  28.             //"On January 1 , 1533 , "
  29.             for (int index = 0; index < line.length(); index++) {
  30.                 char currentSymbol = line.charAt(index);
  31.                 if (currentSymbol == ' ') {
  32.                     continue;
  33.                 }
  34.                 //проверка на символа
  35.                 if (vowels.contains(currentSymbol)) { //гласна буква
  36.                     vowelsCount++;
  37.                 } else if (punctuationalMarks.contains(currentSymbol)) { //пунктуационен знак
  38.                     punctCount++;
  39.                 } else { //съгласна буква
  40.                     consonantsCount++;
  41.                 }
  42.             }
  43.         }
  44.         //знаем броят на символите във всяка категория
  45.         BufferedWriter writer = new BufferedWriter(new FileWriter("output_task_4.txt"));
  46.         writer.write("Vowels: " + vowelsCount);
  47.         writer.newLine();
  48.         writer.write("Consonants: " + consonantsCount);
  49.         writer.newLine();
  50.         writer.write("Punctuation: " + punctCount);
  51.         writer.close();
  52.  
  53.     }
  54.  
  55.     private static Set<Character> getPuntMarks() {
  56.         Set<Character> marks = new HashSet<>();
  57.         marks.add('!');
  58.         marks.add('?');
  59.         marks.add('.');
  60.         marks.add(',');
  61.         return marks;
  62.     }
  63.  
  64.     private static Set<Character> getVowels() {
  65.         Set<Character> vowels = new HashSet<>();
  66.         vowels.add('a');
  67.         vowels.add('e');
  68.         vowels.add('i');
  69.         vowels.add('o');
  70.         vowels.add('u');
  71.         return vowels;
  72.     }
  73. }
  74.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement