Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package WordCount;
- import java.io.*;
- import java.util.HashMap;
- import java.util.Scanner;
- public class WordCounting {
- public static void main(String[] args) throws IOException {
- Scanner SearchWords = new Scanner(new File("C:/Users/User/IdeaProjects/Coding/Words.txt"));
- HashMap<String, Integer> Words = new HashMap<>();
- while (SearchWords.hasNextLine()) {
- Words.put(SearchWords.nextLine().toLowerCase(), 0);
- }
- SearchWords.close();
- Scanner SampleText = new Scanner(new File("C:/Users/User/IdeaProjects/Coding/Sample.txt"));
- while (SampleText.hasNext()) {
- String word = SampleText.next().toLowerCase();
- if (Words.containsKey(word)) {
- int count = Words.get(word) + 1;
- Words.put(word, count);
- }
- }
- File Result = new File("C:/Users/User/IdeaProjects/Result.txt");
- FileWriter FW = new FileWriter(Result);
- PrintWriter PW = new PrintWriter(FW);
- PW.write("Брой на търсените думи: \n");
- PW.write(String.valueOf(Words));
- PW.close();
- System.out.println("Брой на търсените думи: \n" + Words);
- }
- }
Add Comment
Please, Sign In to add comment