Advertisement
Dimitar182

WordsCounting

Jun 9th, 2021
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.44 KB | None | 0 0
  1. import java.io.IOException;
  2. import java.io.File;
  3. import java.util.HashMap;
  4. import java.util.Locale;
  5. import java.util.Scanner;
  6. public class wordscounting {
  7. public static void main(String[] args) throws IOException {
  8. HashMap<String, Integer> WordsCounter = new HashMap<>();
  9.  
  10. Scanner wordsFile = new Scanner(new File("C:\\Users\\cveto\\IdeaProjects\\algoritmi\\src\\words.txt"));
  11. int counter;
  12.  
  13. while (wordsFile.hasNextLine()) {
  14.  
  15. counter = 0;
  16. String searchedWord = wordsFile.nextLine().toLowerCase();
  17. WordsCounter.put(searchedWord, counter);
  18.  
  19. Scanner sampleFile = new Scanner(new File("C:\\Users\\cveto\\IdeaProjects\\algoritmi\\src\\sample.txt"));
  20. while (sampleFile.hasNext()){
  21. String searhedText = sampleFile.next().toLowerCase();
  22. if (searhedText.contains(searchedWord)){
  23. counter++;
  24. }
  25. }//end of while for sample.txt
  26. sampleFile.close();
  27. }//end of while for words.txt
  28. wordsFile.close();
  29.  
  30. Scanner resultFile = new Scanner(new File("C:\\Users\\cveto\\IdeaProjects\\algoritmi\\src\\result.txt"));
  31. while (resultFile.hasNextLine()) {
  32. String printText = resultFile.nextLine();
  33. System.out.println(printText);
  34. }//end of result
  35. resultFile.close();
  36.  
  37. }//end of main
  38. }//end of class
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement