From d158bab65e93a711809e01ab6b72af1da064dfda Mon Sep 17 00:00:00 2001 From: Ramon Torres Date: Fri, 26 Jul 2024 13:43:13 -0500 Subject: [PATCH] updated --- src/main/java/uta/cse3310/WordList.java | 99 +++++++++++++++++++------ 1 file changed, 75 insertions(+), 24 deletions(-) diff --git a/src/main/java/uta/cse3310/WordList.java b/src/main/java/uta/cse3310/WordList.java index c09022f..6fa7aa7 100644 --- a/src/main/java/uta/cse3310/WordList.java +++ b/src/main/java/uta/cse3310/WordList.java @@ -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 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 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 getWords() { - return new ArrayList<>(words); + + + + public static String readWordsFromFile(String filePath)//2 + { + ArrayList 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 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 getArrList() + { + return randomwords; } }