jeudi 30 septembre 2021

Java: I am trying to generate a word with random characters and then checking it against a hashmap of words to see if it is real

So, I am trying to generate a word and then check if it is a real word using word index. Can someone help me figure out how to solve this, it is constantly giving me a never-ending loop. I just simply wanted to create random characters, and after each character see if it exists in the hashmap. First by checking to find the key which is the first letter of the word, and then by checking the substring of each word in that key selection.

  public ArrayList<String> randomSize(int length) {
            ArrayList<String> randomWords = new ArrayList<>(length);
            String letters = "abcdefghijklmnopqrstuvwxyz";
            // for loop add a new slot into randomWords
            for (int eachSlot = 0; eachSlot < length; eachSlot++) {
                // for each slot generate a random length for the word
                int randWordLength = (int) (Math.random() * (10 - 0) + 0);
                // every slot generate a random firstLetter
                int slotFound = 0;
                String firstConfirmedLetter = "";
                // while the first letter is not found in WordIndex
                while (slotFound == 0) {
                    int randNumer = (int) (Math.random() * (24 - 0) + 0);
                    String firstLetter = letters.substring(randNumer, randNumer + 1);
                    if (wordIndex.containsKey(firstLetter) == true)
                    {
                        firstConfirmedLetter = firstLetter;
                        randomWords.add(firstConfirmedLetter);
                        System.out.println(firstLetter);// working
                        randWordLength--;
                        slotFound = 1;
                        // if it is found end the while loop
                    }
    
                }
    
                // we found the first letter, now we need to find the rest of the letters
                for (int eachLetter = 0; eachLetter < randWordLength; eachLetter++){
                    int isFound = 0;
                    // while letter is not found loop through until it is found with combimation to the previous letter
                    while (isFound == 0){
                        // gerate a random letter
                        int randLetter = (int) (Math.random() * (24 - 0) + 0);
                        String nextLetter = letters.substring(randLetter, randLetter + 1);
                        //create curr word
                        String currWord = randomWords.get(eachSlot) + nextLetter;// works until here
                        // loop through each word in wordIdex to find match
                        System.out.println(wordIndex.get(firstConfirmedLetter).size());
                        for(int i = 0; i< wordIndex.get(firstConfirmedLetter).size(); i++){
                            String test = wordIndex.get(firstConfirmedLetter).get(i);
                            if(test.length() > eachLetter+2){
                              System.out.println(test.substring(0,eachLetter+2));
                              if(test.substring(0,eachLetter+2).equals(currWord)){
                                  String currState = randomWords.get(eachSlot);
                                  randomWords.set(eachSlot,currWord);
                                  isFound =1;
                              }
                            }
                        }
    
                    }
                }
    
    
    
            }
            return randomWords;
        }



Aucun commentaire:

Enregistrer un commentaire