Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.File;
- import java.io.FileNotFoundException;
- import java.util.ArrayList;
- import java.util.Scanner;
- public class WordLists
- {
- ArrayList<String> words = new ArrayList<String>();
- public WordLists(String fileName) throws FileNotFoundException //a constructor that takes the name of the dictionary file as the only parameter.
- {
- //fileName = "dictionary.txt";
- Scanner sc = new Scanner(new File(fileName));
- while (sc.hasNextLine())
- {
- words.add(sc.nextLine());
- }
- }
- public ArrayList<String> lengthN(int n) //returns an array of words of length n.
- {
- int i = 0;
- int j = 0;
- int characterLength = n;
- String wordLength = words.get(i);
- ArrayList<String> arrayWithWordsOfParticularLength = new ArrayList<String>();
- if(wordLength.length() == characterLength)
- {
- arrayWithWordsOfParticularLength.set(j, words.get(i));
- i++;
- j++;
- }
- else
- {
- i++;
- }
- return arrayWithWordsOfParticularLength;
- }
- public ArrayList<String> startsWith(int n, char firstLetter) //returns an array of words of length n beginning with the letter firstLetter
- {
- int i = 0;
- int j = 0;
- int characterLength = n;
- String wordLength = words.get(i);
- ArrayList<String> arrayWithWordsOfParticularLengthAndLetter = new ArrayList<String>();
- if(wordLength.length() == characterLength && wordLength.charAt(0) == firstLetter)
- {
- arrayWithWordsOfParticularLengthAndLetter.set(j, words.get(i));
- i++;
- j++;
- }
- else
- {
- i++;
- }
- return arrayWithWordsOfParticularLengthAndLetter;
- }
- 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.
- {
- int i = 0;
- int j = 0;
- String x = "" + included;
- int characterLength = n;
- String wordLength = words.get(i);
- ArrayList<String> arrayWithWordsOfParticularLengthAndLetterAndContains = new ArrayList<String>();
- if(wordLength.length() == characterLength && wordLength.contains(x))
- {
- arrayWithWordsOfParticularLengthAndLetterAndContains.set(j, words.get(i));
- i++;
- j++;
- }
- else
- {
- i++;
- }
- return arrayWithWordsOfParticularLengthAndLetterAndContains;
- }
- public ArrayList<String> vowelHeavy(int n, int m) //returns an array of words of length n containing at least m vowels.
- {
- int i = 0;
- int j = 0;
- int q = 0;
- char a = 'a';
- char e = 'e';
- char l = 'i';
- char o = 'o';
- char u = 'u';
- char y = 'y';
- int characterLength = n;
- int vowelCounter = 0;
- String wordLength = words.get(i);
- char cap = wordLength.charAt(q); // character at position
- ArrayList<String> arrayWithWordsOfParticularLengthAndVowels = new ArrayList<String>();
- while(i <= words.size())
- {
- if(wordLength.length() == characterLength)
- {
- while(vowelCounter <= m && q <= wordLength.length())
- {
- if(cap == a || cap == e || cap == l || cap == o || cap == u || cap == y && vowelCounter <= m)
- {
- vowelCounter++;
- q++;
- }
- else
- {
- q++;
- }
- }
- if(vowelCounter >= m && wordLength.length() == characterLength)
- {
- arrayWithWordsOfParticularLengthAndVowels.set(j, words.get(i));
- i++;
- j++;
- vowelCounter = 0;
- }
- }
- else
- {
- i++;
- }
- }
- return arrayWithWordsOfParticularLengthAndVowels;
- }
- public ArrayList<String> multiLetter(int m, char included) //returns an array of words with at least m occurrences of the letter included.
- {
- int i = 0;
- int j = 0;
- int q = 0;
- int occurenceCounter = 0;
- String wordLength = words.get(i);
- char cap = wordLength.charAt(q); // character at position
- ArrayList<String> arrayWithWordsOfParticularNumberOfCharacters = new ArrayList<String>();
- while(i <= words.size())
- {
- while(occurenceCounter <= m)
- {
- if(cap == included && occurenceCounter <= m)
- {
- occurenceCounter++;
- q++;
- }
- else
- {
- q++;
- }
- }
- if(occurenceCounter >= m)
- {
- arrayWithWordsOfParticularNumberOfCharacters.set(j, words.get(i));
- i++;
- j++;
- occurenceCounter = 0;
- }
- else
- {
- i++;
- }
- }
- return arrayWithWordsOfParticularNumberOfCharacters;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement