For now this has a sample dictionary of words, but later I will allow the user to provide their own dictionary.

import java.io.*;

public class testReader
{
 private String[] auto = {"lorem", "ipsum", "dolor", "sunglasses", "friend", "time", "flies", "like", "an", "arrow", "daisy", "bell", "the", "quick", "brown", "fox", "jumps", "over", "lazy", "yellow", "dog"};
 private String guess = "";
 
   public testReader()
   {
     try
     {
       BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
       String strLine;
    
         while ((strLine = reader.readLine()) != null)
         {
           while (strLine.length() >= 3)
           {
             for (String com : auto)
             {
               if ((com.substring(0, 2)).equals(strLine.substring(0, 2)))
               {
                 myWindow.guess(com);
                 // if 'yes'
                 myWindow.guess(com);
                 // else
                 // myWindow.guess("unknown");
                 
               } else {
                myWindow.guess("unknown");
                
               }
             
           }
      
         }
           
       }

    
    } catch (Exception e) {}
   }

    
    public static void main(String[] args) throws Exception
    {
        
      new testReader();
    }

}

Anyway, this code is not runnable. The VM just freezes when I try to execute this code... Although it does not work right now, it is supposed to get user input (while a word is being typed) compare it to the dictionary words, and guess (based on the first three letters typed) which word the user is going to type at which time the program displays the guess on a window I made (dummy window myWindow).. I also do not want the text area on myWindow, or moved anywhere else.

How can I fix this code so that it will work as explained above?

Recommended Answers

All 3 Replies

you can use the startsWith(); method, which checks if the word starts with the letters entered. for example

private String[] auto = {"lorem", "ipsum", "dolor", "sunglasses", "friend", "time", "flies", "like", "an", "arrow", "daisy", "bell", "the", "quick", "brown", "fox", "jumps", "over", "lazy", "yellow", "dog"};

public static void main (String [] args){

     String input = userInput.readLine ();

     for (String com : auto){
          if (com.startsWith (input)){
               System.out.println (com + " Starts With " + input);
          }
     }

}

of course this code will not run, I just showed you how to compare the beginning of the word to the user inputed text. This application would work better if it was a gui application, rather then a command line application, because you could update the words as the user types into the JTextField.

The VM just freezes

Your code is probably in a tight loop. Are there any loops in your code that might not end?
For debugging, add an int variable that you increment inside the loop and exit the loop when its value exceeds a maximum value.
if(lpCnt++) > 100)
break; // Exit loop if more than 100 iterations

Also if you don't see why you are looking add some printlns inside of the loop that print out the loop controlling variable's value.

while (strLine.length() >= 3)

Once this loop has started, under what circumstances will it ever stop?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.