Advertisement
Guest User

Untitled

a guest
Dec 18th, 2017
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.69 KB | None | 0 0
  1. public class Main {
  2.     public static void main(String[] args) {   
  3. try {
  4.             Task5();
  5.         } catch (IOException e) {
  6.             System.out.println(e.getMessage());
  7.         }
  8.  
  9.     }
  10. public static void Task5() throws IOException {
  11.         BufferedReader in = null;
  12.         BufferedReader in1 = null;
  13.         BufferedWriter out = null;
  14.         String[] words = null;
  15.         HashMap<String, Integer> results = new HashMap<>();
  16.         try {
  17.             in = new BufferedReader(new FileReader("words.txt"));
  18.             in1 = new BufferedReader(new FileReader("text.txt"));
  19.             out = new BufferedWriter(new FileWriter("results.txt"));
  20.             StringBuilder word = new StringBuilder();
  21.             String currLine = null;
  22.             while ((currLine = in.readLine()) != null) {
  23.                 word.append(currLine);
  24.             }
  25.             words = word.toString().split(",");
  26.             currLine = null;
  27.             while ((currLine = in1.readLine()) != null) {
  28.                 for (String s : words) {
  29.                     Pattern p = Pattern.compile(s);
  30.                     Matcher m = p.matcher(currLine);
  31.                     if (currLine.contains(s)) {
  32.                         int br = 0;
  33.                         while (m.find()) {
  34.                             br++;
  35.                         }
  36.                         if (results.containsKey(s)) {
  37.                             Integer count = results.get(s);
  38.                             results.put(s, count + br);
  39.                         } else {
  40.                             results.put(s, br);
  41.                         }
  42.                     }
  43.  
  44.                 }
  45.             }
  46.             for (Entry e : results.entrySet()) {
  47.                 out.write(e.getKey() + " " + e.getValue() + " times" + System.getProperty("line.separator"));
  48.             }
  49.         } catch (FileNotFoundException e) {
  50.             System.out.println(e.getMessage());
  51.         } catch (IOException e) {
  52.             throw new IOException("I/O stream error!");
  53.         } finally {
  54.             if (in != null) {
  55.                 in.close();
  56.             }
  57.             if (in1 != null) {
  58.                 in.close();
  59.             }
  60.             if (out != null) {
  61.                 out.close();
  62.             }
  63.         }
  64.         System.out.println("Done");
  65.     }
  66. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement