I am writing a Scrabble game and trying to write a method that gets a word and compares all the characters of that word to a word in a file with over 200K+ words to see if they match. For example, take two words: ABBAY and BAB. ABBAY contains two possible combinations of all the letters of BAB. It contains AB and BA.
This is the code I have thus far, which gets a word and makes it a character array, sorts it by alphabetical order, and puts it back into a string
Then gets every single word in the text file, and does the same process outlined above to sort it by alphabetical order
Using the if statements it asks if the two words are equal, and if they are, add them to my ArrayList. However, all of this has not worked correctly for me.
public ArrayList<String> getWords() throws FileNotFoundException, IOException {
FileReader reader = new FileReader("CollinsScrabbleWords2019.txt");
BufferedReader data = new BufferedReader(reader);
String[] listOfWords = new String[279496];
ArrayList<String> list = new ArrayList<>();
char[] charArray = getLetters().toCharArray();
char[] wordInText;
Arrays.sort(charArray);
int i=0;
while(data.ready()){
listOfWords[i++] = data.readLine();
}
for (int x = 0; x < listOfWords.length; x++) {
wordInText = listOfWords[x].toCharArray();
Arrays.sort(wordInText);
if (new String(charArray).equalsIgnoreCase(new String(wordInText))) {
list.add(listOfWords[x]);
}
}
return list;
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.