From 869f413a74cbd378de6fc362f1c7261696d2782c Mon Sep 17 00:00:00 2001 From: 2onefan2 <159247397+2onefan2@users.noreply.github.com> Date: Sun, 28 Apr 2024 21:25:54 -0500 Subject: [PATCH] Fixed CPU-intensive infinite loop in WordList generation process. --- src/main/java/uta/cse3310/WordList.java | 48 ++++++++++++------------- 1 file changed, 22 insertions(+), 26 deletions(-) diff --git a/src/main/java/uta/cse3310/WordList.java b/src/main/java/uta/cse3310/WordList.java index 0739692..b8e0f2c 100644 --- a/src/main/java/uta/cse3310/WordList.java +++ b/src/main/java/uta/cse3310/WordList.java @@ -1,5 +1,6 @@ package uta.cse3310; +import java.io.IOException; import java.io.*; import java.util.ArrayList; import java.util.Collections; @@ -8,55 +9,50 @@ import java.nio.file.Path; import java.nio.file.Paths; -public class WordList //extends Direction -{ +public class WordList { public static List list; - //private [] words; - //method for accesing the word list from file - public static ArrayList getWordList(String wordsFile) - { + public static ArrayList getWordList(String wordsFile) { ArrayList wordList = new ArrayList<>(); - + try { - list = Files.readAllLines(Paths.get(wordsFile)); + List lines = Files.readAllLines(Paths.get(wordsFile)); + if (lines.isEmpty()) { + System.out.println("The file is empty: " + wordsFile); + return wordList; // Return empty list if the file is empty + } + + list = new ArrayList<>(lines); shuffleWords(); wordList.addAll(list); } catch (IOException e) { - System.out.println("not a valid file!"); + System.out.println("Error reading the file: " + wordsFile); + e.printStackTrace(); // Print the stack trace for debugging } - + return wordList; } - //method for word shuffling - public static void shuffleWords() - { - Collections.shuffle(list); + public static void shuffleWords() { + Collections.shuffle(list); } - public static ArrayList updatedWordList(ArrayList wordBank) - { + public static ArrayList updatedWordList(ArrayList wordBank) { int totalCharacters = 0; int index = 0; ArrayList updatedList = new ArrayList<>(); - while (totalCharacters < 500 && index < wordBank.size()) - { + while (totalCharacters < 500 && index < wordBank.size()) { String word = wordBank.get(index); - if (totalCharacters < 500) - { + if (totalCharacters + word.length() <= 500) { updatedList.add(word); totalCharacters += word.length(); - } else - { + } else { break; } index++; } - + return updatedList; } - -} - +} \ No newline at end of file