Advertisement
gluten_free_feeling

A19 CountWords

Mar 24th, 2016
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.30 KB | None | 0 0
  1. import java.util.*;
  2. import java.io.*;
  3. //ANDY ZHANG, 2
  4. public class CountWords
  5. {
  6. ArrayList <String> list = new ArrayList <String> ();
  7. ArrayList <String> listCopy = new ArrayList <String> ();
  8. ArrayList <String> word = new ArrayList <String> ();
  9. ArrayList <Integer> wordCount = new ArrayList <Integer> ();
  10. int count;
  11. public void countAndStore()
  12. {
  13. count = 0;
  14. try {
  15. Scanner in = new Scanner(new File("Lincoln - a19.3.txt"));
  16. while(in.hasNext())
  17. {
  18. String temp = in.next();
  19. temp = temp.replaceAll("[^a-zA-Z ]", "").toLowerCase();
  20. if(temp.length() >= 1)
  21. {
  22. //System.out.println(temp);
  23. list.add(temp);
  24. listCopy.add(temp);
  25. count++;
  26. }
  27. else
  28. {
  29. //nothing
  30. }
  31. }
  32. } catch (Exception i) {
  33. System.out.println(i);
  34. }
  35. System.out.println(count);
  36. for(String s: list)
  37. {
  38. System.out.print(s + " ");
  39. }
  40.  
  41. }
  42.  
  43. public void countIndividual()
  44. {
  45.  
  46. while(list.size() > 0)
  47. {
  48. int index = 0;
  49. int counter = 1;
  50. String store = list.get(0);
  51. //
  52. word.add(store);
  53. System.out.println(store);
  54. list.remove(index);
  55. while(index < list.size())
  56. {
  57. String temp = list.get(index);
  58. if(temp.equals(store))
  59. {
  60. counter++;
  61. list.remove(index);
  62. }
  63. index++;
  64. }
  65. wordCount.add(counter);
  66. }
  67. }
  68.  
  69. public void search(String word)
  70. {
  71. int c = 0;
  72. for(int x = 0; x < listCopy.size(); x++)
  73. {
  74. String temp = listCopy.get(x);
  75. if(temp.equalsIgnoreCase(word))
  76. System.out.print("True with " + c + " searches of sequential");
  77. else
  78. System.out.print("False, word does not exist");
  79. c++;
  80. }
  81. System.out.println();
  82. }
  83.  
  84. public void printer()
  85. {
  86. System.out.println();
  87. for(int x = 0; x < 30; x++)
  88. {
  89. System.out.printf("%-14s", word.get(x));
  90. System.out.print(" : ");
  91. System.out.printf("%s", wordCount.get(x));
  92. System.out.println();
  93. }
  94. }
  95. public void top()
  96. {
  97. int max;
  98. String temp;
  99. int temp1;
  100. for (int outer = 0; outer < word.size() - 1; outer++){
  101. max = outer;
  102. for (int inner = outer + 1; inner < word.size(); inner++){
  103. if (wordCount.get(inner) > wordCount.get(max)) {
  104. max = inner; // a new smallest item is found
  105. }
  106. }
  107. //swap word[outer] & word[max]
  108. temp = word.get(outer);
  109. word.set(outer, word.get(max));
  110. word.set(max, temp);
  111. //
  112. temp1 = wordCount.get(outer);
  113. wordCount.set(outer, wordCount.get(max));
  114. wordCount.set(max, temp1);
  115. }
  116. }
  117. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement