Skip to content

Commit

Permalink
updated
Browse files Browse the repository at this point in the history
  • Loading branch information
RamonTorresUta committed Jul 26, 2024
1 parent 473c21f commit d158bab
Showing 1 changed file with 75 additions and 24 deletions.
99 changes: 75 additions & 24 deletions src/main/java/uta/cse3310/WordList.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,35 +7,86 @@
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import java.io.BufferedReader;
import java.io.FileReader;

public class WordList {

private List<String> words;

public WordList(String filePath) throws IOException {
loadWords(filePath);
}

private void loadWords(String filePath) throws IOException {
words = Files.readAllLines(Paths.get(filePath));
}
//do "WordList wordlist = new WordList();" access to Wordlist file
//then "wordlist.gatherwords();" to gather words

public String getRandomWord() {
Random rand = new Random();
return words.get(rand.nextInt(words.size()));

public class WordList {

ArrayList<String> randomwords = new ArrayList<>();



public void printwords()
{
for (String word : randomwords)
{
System.out.println("word selected " + word);
}
}



public void gatherwords() //1
{
Random random = new Random();
int randomNumber = random.nextInt(3) + 1;
System.out.println("I am selecting " + randomNumber + " words");

for( int i = 0; i < randomNumber; i++)
{
String w = readWordsFromFile("src/main/resources/words.txt");//2
randomwords.add(w);
}
}

public List<String> getWords() {
return new ArrayList<>(words);



public static String readWordsFromFile(String filePath)//2
{
ArrayList<String> words = new ArrayList<>();
try (BufferedReader br = new BufferedReader(new FileReader(filePath)))
{
String word;
while ((word = br.readLine()) != null)
{
words.add(word);
}
}
catch (IOException e)
{
System.err.println("abort abort failed!!!");
}
return isValidWord(words); //3
}

public static void main(String[] args) {
try {
WordList wordList = new WordList("src/main/resources/words.txt");
System.out.println("Random word: " + wordList.getRandomWord());
} catch (IOException e) {
System.err.println("Error loading words: " + e.getMessage());



public static String isValidWord(ArrayList<String> words)//3
{
String validword = "";
while(validword == "")
{
Random rand = new Random();
int randomIndex = rand.nextInt(words.size());
String testword = words.get(randomIndex);
String rule = "^[a-z]+$";
if(testword.matches(rule) && testword.length() >= 3 && testword.length() <= 12)
{
validword = testword;
}
}
return validword;
}



public ArrayList<String> getArrList()
{
return randomwords;
}
}

0 comments on commit d158bab

Please sign in to comment.