Advertisement
Guest User

Untitled

a guest
Apr 20th, 2014
58
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.22 KB | None | 0 0
  1. import java.io.File;
  2. import java.io.FileNotFoundException;
  3. import java.util.ArrayList;
  4. import java.util.Scanner;
  5.  
  6.  
  7. public class WordLists
  8. {
  9. ArrayList<String> words = new ArrayList<String>();
  10.  
  11. public WordLists(String fileName) throws FileNotFoundException //a constructor that takes the name of the dictionary file as the only parameter.
  12. {
  13. //fileName = "dictionary.txt";
  14. Scanner sc = new Scanner(new File(fileName));
  15. while (sc.hasNextLine())
  16. {
  17. words.add(sc.nextLine());
  18. }
  19.  
  20. }
  21.  
  22. public ArrayList<String> lengthN(int n) //returns an array of words of length n.
  23. {
  24. int i = 0;
  25. int j = 0;
  26. int characterLength = n;
  27. String wordLength = words.get(i);
  28. ArrayList<String> arrayWithWordsOfParticularLength = new ArrayList<String>();
  29. if(wordLength.length() == characterLength)
  30. {
  31. arrayWithWordsOfParticularLength.set(j, words.get(i));
  32. i++;
  33. j++;
  34. }
  35. else
  36. {
  37. i++;
  38. }
  39.  
  40. return arrayWithWordsOfParticularLength;
  41.  
  42. }
  43.  
  44. public ArrayList<String> startsWith(int n, char firstLetter) //returns an array of words of length n beginning with the letter firstLetter
  45. {
  46. int i = 0;
  47. int j = 0;
  48. int characterLength = n;
  49. String wordLength = words.get(i);
  50. ArrayList<String> arrayWithWordsOfParticularLengthAndLetter = new ArrayList<String>();
  51. if(wordLength.length() == characterLength && wordLength.charAt(0) == firstLetter)
  52. {
  53. arrayWithWordsOfParticularLengthAndLetter.set(j, words.get(i));
  54. i++;
  55. j++;
  56. }
  57. else
  58. {
  59. i++;
  60. }
  61.  
  62. return arrayWithWordsOfParticularLengthAndLetter;
  63. }
  64.  
  65. public ArrayList<String> containsLetter(int n, char included) //returns an array of words of length n containing the letter included but not beginning with it.
  66. {
  67. int i = 0;
  68. int j = 0;
  69. String x = "" + included;
  70. int characterLength = n;
  71. String wordLength = words.get(i);
  72. ArrayList<String> arrayWithWordsOfParticularLengthAndLetterAndContains = new ArrayList<String>();
  73. if(wordLength.length() == characterLength && wordLength.contains(x))
  74. {
  75. arrayWithWordsOfParticularLengthAndLetterAndContains.set(j, words.get(i));
  76. i++;
  77. j++;
  78. }
  79. else
  80. {
  81. i++;
  82. }
  83.  
  84. return arrayWithWordsOfParticularLengthAndLetterAndContains;
  85. }
  86.  
  87. public ArrayList<String> vowelHeavy(int n, int m) //returns an array of words of length n containing at least m vowels.
  88. {
  89. int i = 0;
  90. int j = 0;
  91. int q = 0;
  92. char a = 'a';
  93. char e = 'e';
  94. char l = 'i';
  95. char o = 'o';
  96. char u = 'u';
  97. char y = 'y';
  98. int characterLength = n;
  99. int vowelCounter = 0;
  100. String wordLength = words.get(i);
  101. char cap = wordLength.charAt(q); // character at position
  102. ArrayList<String> arrayWithWordsOfParticularLengthAndVowels = new ArrayList<String>();
  103. while(i <= words.size())
  104. {
  105. if(wordLength.length() == characterLength)
  106. {
  107. while(vowelCounter <= m && q <= wordLength.length())
  108. {
  109. if(cap == a || cap == e || cap == l || cap == o || cap == u || cap == y && vowelCounter <= m)
  110. {
  111. vowelCounter++;
  112. q++;
  113. }
  114. else
  115. {
  116. q++;
  117. }
  118. }
  119.  
  120. if(vowelCounter >= m && wordLength.length() == characterLength)
  121. {
  122. arrayWithWordsOfParticularLengthAndVowels.set(j, words.get(i));
  123. i++;
  124. j++;
  125. vowelCounter = 0;
  126. }
  127. }
  128. else
  129. {
  130. i++;
  131. }
  132. }
  133.  
  134. return arrayWithWordsOfParticularLengthAndVowels;
  135. }
  136.  
  137. public ArrayList<String> multiLetter(int m, char included) //returns an array of words with at least m occurrences of the letter included.
  138. {
  139. int i = 0;
  140. int j = 0;
  141. int q = 0;
  142.  
  143. int occurenceCounter = 0;
  144. String wordLength = words.get(i);
  145. char cap = wordLength.charAt(q); // character at position
  146. ArrayList<String> arrayWithWordsOfParticularNumberOfCharacters = new ArrayList<String>();
  147. while(i <= words.size())
  148. {
  149. while(occurenceCounter <= m)
  150. {
  151. if(cap == included && occurenceCounter <= m)
  152. {
  153. occurenceCounter++;
  154. q++;
  155. }
  156. else
  157. {
  158. q++;
  159. }
  160. }
  161.  
  162. if(occurenceCounter >= m)
  163. {
  164. arrayWithWordsOfParticularNumberOfCharacters.set(j, words.get(i));
  165. i++;
  166. j++;
  167. occurenceCounter = 0;
  168. }
  169.  
  170. else
  171. {
  172. i++;
  173. }
  174. }
  175. return arrayWithWordsOfParticularNumberOfCharacters;
  176. }
  177. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement