Advertisement
alyoshinkkaa

Untitled

Aug 1st, 2023
49
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.40 KB | None | 0 0
  1. import java.io.*;
  2. import java.nio.charset.StandardCharsets;
  3. import java.nio.file.NoSuchFileException;
  4. import java.util.Scanner;
  5. import java.util.ArrayList;
  6. import java.util.LinkedHashMap;
  7. import java.util.Map;
  8. public class WordStatInput {
  9.     private static boolean isLegalCharacter(char c) {
  10.         return ((Character.isLetter(c)) || (Character.getType(c) == Character.DASH_PUNCTUATION) || (c == '\''));
  11.     }
  12.  
  13.     public static void main(String[] args) {
  14.  
  15.         Map<String, Integer> wordsStats = new LinkedHashMap<String, Integer>();
  16.         // :NOTE: why do you allocate extra memory for input output files?
  17.         String inputFile = args[0];
  18.         String outputFile = args[1];
  19.  
  20.         try (BufferedReader reader = new BufferedReader(new FileReader(inputFile, StandardCharsets.UTF_8))) {
  21.             // :NOTE: use StringBuilder
  22.             String line;
  23.             // :NOTE: if the input is given a very large string, readLine() will break
  24.             while ((line = reader.readLine()) != null) {
  25.                 int startIndex = 0;
  26.                 // :NOTE: crutch
  27.                 line += " ";
  28.                 for (int index = 0; index < line.length(); index++) {
  29.                     char character = line.charAt(index);
  30.                     if (!isLegalCharacter(character)) {
  31.                         // :NOTE: same
  32.                         String word = line.substring(startIndex, index).toLowerCase();
  33.                         if (!word.isEmpty()) {
  34.                             // :NOTE: use getOrDefault method instead of if else
  35.                             if (!wordsStats.containsKey(word)) {
  36.                                 wordsStats.put(word, 1);
  37.                             } else {
  38.                                 // :NOTE: extra memory
  39.                                 Integer wordOccurence = (Integer) wordsStats.get(word);
  40.                                 wordsStats.put(word, wordOccurence + 1);
  41.                             }
  42.                         }
  43.                         startIndex = index + 1;
  44.                     }
  45.                 }
  46.             }
  47.         } catch (FileNotFoundException e) {
  48.             // :NOTE: user wants to read the detailed message of the throwable exception (use getMessage)
  49.             System.out.println("Unable to access file");
  50.         } catch (IllegalArgumentException e) {
  51.             // :NOTE: same
  52.             System.out.println("Passed an unsuitable argument");
  53.         } catch (IOException e) {
  54.             // :NOTE: same
  55.             System.out.println("Unable to read file");
  56.         }
  57.  
  58.         try (BufferedWriter out = new BufferedWriter(new FileWriter(outputFile, StandardCharsets.UTF_8))) {
  59.             for (Map.Entry<String, Integer> entry : wordsStats.entrySet()) {
  60.                 out.write(entry.getKey() + " " + entry.getValue() + System.lineSeparator().toString());
  61.             }
  62.             // :NOTE: you can use general try to catch same exceptions
  63.         } catch (IllegalArgumentException e) {
  64.             // :NOTE: same
  65.             System.out.println("Passed an unsuitable argument");
  66.             // :NOTE: why do you use NoSuchFile? maybe it is better to use FileNotFound?
  67.         } catch (NoSuchFileException e) {
  68.             // :NOTE: same
  69.             System.out.println("File does not exist");
  70.         } catch (IOException e) {
  71.             // :NOTE: same
  72.             System.out.println("Unable to write file");
  73.         }
  74.     }
  75. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement