Denis_Hristov

WordsCounter

Apr 25th, 2021 (edited)
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.25 KB | None | 0 0
  1. package WordCount;
  2.  
  3. import java.io.*;
  4. import java.util.HashMap;
  5. import java.util.Scanner;
  6.  
  7. public class WordCounting {
  8.     public static void main(String[] args) throws IOException {
  9.         Scanner SearchWords = new Scanner(new File("C:/Users/User/IdeaProjects/Coding/Words.txt"));
  10.  
  11.         HashMap<String, Integer> Words = new HashMap<>();
  12.  
  13.         while (SearchWords.hasNextLine()) {
  14.             Words.put(SearchWords.nextLine().toLowerCase(), 0);
  15.         }
  16.  
  17.         SearchWords.close();
  18.  
  19.         Scanner SampleText = new Scanner(new File("C:/Users/User/IdeaProjects/Coding/Sample.txt"));
  20.  
  21.         while (SampleText.hasNext()) {
  22.             String word = SampleText.next().toLowerCase();
  23.             if (Words.containsKey(word)) {
  24.                 int count = Words.get(word) + 1;
  25.                 Words.put(word, count);
  26.             }
  27.         }
  28.  
  29.         File Result = new File("C:/Users/User/IdeaProjects/Result.txt");
  30.         FileWriter FW = new FileWriter(Result);
  31.         PrintWriter PW = new PrintWriter(FW);
  32.         PW.write("Брой на търсените думи:  \n");
  33.         PW.write(String.valueOf(Words));
  34.         PW.close();
  35.        
  36.         System.out.println("Брой на търсените думи:  \n" + Words);
  37.     }
  38. }
  39.  
Add Comment
Please, Sign In to add comment