Advertisement
alyoshinkkaa

Untitled

Jul 29th, 2023
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.33 KB | None | 0 0
  1. import java.io.BufferedReader;
  2. import java.io.BufferedWriter;
  3. import java.io.FileInputStream;
  4. import java.io.InputStreamReader;
  5. import java.io.Reader;
  6. import java.io.FileNotFoundException;
  7. import java.io.IOException;
  8. import java.io.OutputStreamWriter;
  9. import java.io.FileOutputStream;
  10. import java.lang.String;
  11. import java.util.LinkedHashMap;
  12. import java.util.Map;
  13.  
  14. public class WordStatInput {
  15.     public static Map<String, Integer> addWord(Map<String, Integer> words, String word) {
  16.         // :NOTE: without if, use .getOrDefault() method
  17.         if (words.containsKey(word)) {
  18.             words.replace(word, words.get(word), words.get(word) + 1);
  19.         } else {
  20.             if (word.length() > 0) {
  21.                 words.put(word, 1);
  22.             }
  23.         }
  24.         return words;
  25.     }
  26.     public static void main(String[] args){
  27.         Map<String, Integer> words = new LinkedHashMap<>();
  28.         try {
  29.             Reader reader = new BufferedReader(new InputStreamReader(
  30.                     new FileInputStream(args[0])
  31.             ));
  32.             try {
  33.                 char symbol;
  34.                 StringBuilder word = new StringBuilder();
  35.                 int read = reader.read();
  36.                 symbol = (char) read;
  37.                 while (read > -1) {
  38.                     if (Character.isLetter(symbol) || symbol == '\'' || Character.DASH_PUNCTUATION == Character.getType(symbol)) {
  39.                         word.append(symbol);
  40.                     } else {
  41.                         // :NOTE: Map is passed by reference
  42.                         words = addWord(words, word.toString().toLowerCase());
  43.                         word.delete(0, word.length());
  44.                     }
  45.                     read = reader.read();
  46.                     symbol = (char) read;
  47.                 }
  48.                 // :NOTE: same
  49.                 words = addWord(words, word.toString().toLowerCase());
  50.             // :NOTE: FileNotFoundException can't appear here
  51.             } catch (FileNotFoundException e) {
  52.                 System.out.println(e.getMessage());
  53.             } finally {
  54.                 reader.close();
  55.             }
  56.         // :NOTE: IOException can catch not only "file not found"
  57.         } catch (IOException e) {
  58.             System.out.println("Input File Not Found: " + e.getMessage());
  59.         }
  60.         // :NOTE: put this block of code in first "try", why do you separate them?
  61.         try {
  62.             BufferedWriter writer = new BufferedWriter(
  63.                     new OutputStreamWriter(
  64.                             new FileOutputStream(args[1])
  65.                     )
  66.             );
  67.             try {
  68.                 for (Map.Entry<String, Integer> output : words.entrySet())
  69.                 {
  70.                     writer.write(output.getKey() + " " + output.getValue());
  71.                     // :NOTE: put lineSeparator in the previous .write() call, why do you separate them?
  72.                     writer.write(System.lineSeparator());
  73.                 }
  74.             } catch (FileNotFoundException e) {
  75.                 System.out.println("Output File Not Found: " + e.getMessage());
  76.             } finally {
  77.                 writer.close();
  78.             }
  79.         // :NOTE: same
  80.         } catch (IOException e) {
  81.             System.out.println("Output File Not Found: " + e.getMessage());
  82.         }
  83.     }
  84. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement