Advertisement
TsetsoP

hashmap_acho

Apr 22nd, 2021
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.13 KB | None | 0 0
  1. package wordsCount;
  2.  
  3. import java.util.*;
  4. import java.io.*;
  5.  
  6. public class wcounter {
  7.  
  8. public static void main(String[] args)
  9.  
  10. throws FileNotFoundException {
  11.  
  12. HashMap <String, Integer> wordCounter = new HashMap<String, Integer>();
  13.  
  14. Scanner wordsFile = new Scanner(new File("words.txt"));
  15.  
  16. Scanner sampleFile = new Scanner(new File("sample.txt"));
  17.  
  18. String words[];
  19.  
  20. int counter = 0;
  21.  
  22. while (wordsFile.hasNextLine()) {
  23.  
  24. String searchedWord = wordsFile.nextLine().toLowerCase();
  25.  
  26. wordCounter.put(searchedWord, 0);
  27. }
  28.  
  29. wordsFile.close();
  30.  
  31. int[] wordsCount = new int[words.length];
  32.  
  33. while (sampleFile.hasNext()) {
  34.  
  35. String sampleWord = sampleFile.next().toLowerCase();
  36.  
  37. for (String word : words) {
  38.  
  39. if (sampleWord.contains(word)) {
  40.  
  41. counter++;
  42.  
  43. }
  44.  
  45. }
  46.  
  47. }
  48.  
  49. sampleFile.close();
  50. PrintStream resultFile = new PrintStream("result.txt");
  51.  
  52. for (String word : words) {
  53.  
  54. resultFile.format("%s - %s%n", word,wordsCount[words.indexOf(word)]);
  55. // тук ми дава грешка
  56.  
  57. }
  58.  
  59. resultFile.close();
  60.  
  61. }
  62.  
  63. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement