Advertisement
Guest User

Untitled

a guest
Feb 24th, 2020
115
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.01 KB | None | 0 0
  1. package procesare;
  2.  
  3. import java.io.BufferedReader;
  4. import java.io.File;
  5. import java.io.FileReader;
  6. import java.io.IOException;
  7. import java.util.Hashtable;
  8. import java.util.Map.Entry;
  9. import java.util.Hashtable.*;
  10.  
  11. public class ExceptionsAndStopWords {
  12.  
  13. //hash table pentru stocarea cuvintelor si nr de apritii ale lor
  14. private Hashtable<String, Integer> table = new Hashtable<String, Integer>();
  15.  
  16. //constructorul
  17. public ExceptionsAndStopWords() {}
  18.  
  19. //functia care returneaza un buuffered reader pe baza unui fisier dat ca parametru
  20. public BufferedReader OpenFile(String name)
  21. {
  22. try
  23. {
  24. File file = new File(name);
  25. FileReader fr = new FileReader(file);
  26. BufferedReader br = new BufferedReader(fr);
  27. return br;
  28. }catch (IOException e) {
  29. e.printStackTrace();
  30. BufferedReader br = null;
  31. return br;
  32. }
  33. }
  34.  
  35. //functia care creaza un hashtable
  36. public void CreatingHashTable()
  37. {
  38. BufferedReader br = OpenFile("C:\\Users\\Gabi Socea\\Desktop\\Java\\RIW - Aplicatii\\Laborator_1\\Count_words\\file_input.txt");
  39. int c = 0;
  40. String words="";
  41.  
  42. try
  43. {
  44. while((c = br.read()) != -1)
  45. {
  46. char ch = (char) c;
  47. if((ch >= 'A' && ch<='Z') || (ch>='a' && ch<='z') || (ch>='0' && ch<='9'))
  48. {
  49. words+=ch;
  50. }
  51. else
  52. {
  53. if(words.length() >= 1)
  54. {
  55. Integer count = table.get(words);
  56. table.put(words, count == null ? 1 : (count+1));
  57. }
  58. words = "";
  59.  
  60. }
  61. }
  62. }catch (IOException e) {
  63. e.printStackTrace();
  64. }
  65. }
  66.  
  67. //functia care afiseaza Hash Table-ul
  68. public void DisplayHashTable()
  69. {
  70. for(Entry<String, Integer> entry : table.entrySet())
  71. {
  72. System.out.println(entry.getKey() + "\t" + entry.getValue());
  73. }
  74. }
  75.  
  76. public static void main(String[] args) {
  77. }
  78.  
  79. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement