desislava_topuzakova

4. Count Character Types

Jun 1st, 2021
1,433
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.33 KB | None | 0 0
  1. import jdk.swing.interop.SwingInterOpUtils;
  2.  
  3. import java.io.*;
  4. import java.nio.file.Files;
  5. import java.nio.file.NoSuchFileException;
  6. import java.nio.file.Path;
  7. import java.util.*;
  8.  
  9. public class Demo {
  10.     public static void main(String[] args) throws IOException {
  11.         //1. Read data
  12.         Path path = Path.of("C:\\Users\\I353529\\Documents\\SoftUni\\Exercises Resources\\input.txt");
  13.         List<String> lines = new ArrayList<>();
  14.         try {
  15.             lines = Files.readAllLines(path);
  16.         } catch (Exception io) {
  17.             System.out.println("Another error with reading file");
  18.         }
  19.         //2. преброим гласни, съгласни, пунк. знаци
  20.  
  21.         Map<String, Integer> symbols = new HashMap<>();
  22.         //гласни -> брой
  23.         symbols.put("vowels", 0); //a e i o u
  24.         //съгласни -> брой
  25.         symbols.put("consonants", 0);
  26.         //пунк.знаци -> брой
  27.         symbols.put("punctuations", 0); //! , . ?
  28.  
  29.         for (String line : lines) {
  30.             for (char symbol : line.toCharArray()) {
  31.                 if(symbol == ' ') {
  32.                     continue;
  33.                 } else if (isVowel(symbol)) {
  34.                     symbols.put("vowels", symbols.get("vowels") + 1);
  35.                 } else if (isPunctional(symbol)) {
  36.                     symbols.put("punctuations", symbols.get("punctuations") + 1);
  37.                 } else {
  38.                     symbols.put("consonants", symbols.get("consonants") + 1);
  39.                 }
  40.             }
  41.         }
  42.         //3. print
  43.         BufferedWriter writer = new BufferedWriter(new FileWriter("output_task_4.txt"));
  44.         writer.write("Vowels: " + symbols.get("vowels"));
  45.         writer.newLine();
  46.         writer.write("Consonants: " + symbols.get("consonants"));
  47.         writer.newLine();
  48.         writer.write("Punctuation: " + symbols.get("punctuations"));
  49.  
  50.         writer.close();
  51.  
  52.     }
  53.  
  54.     private static boolean isPunctional(char symbol) {
  55.         //! , . ?
  56.  
  57.         return symbol == '!' || symbol == ',' || symbol == '.' || symbol == '?';
  58.     }
  59.  
  60.     private static boolean isVowel(char symbol) {
  61.         //true -> гласна
  62.         //false -> съгласна
  63.         return symbol == 'a' || symbol == 'e' || symbol == 'i' || symbol == 'o' || symbol == 'u';
  64.     }
  65.  
  66. }
  67.  
Advertisement
Add Comment
Please, Sign In to add comment