Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 25 of 25

Thread: Logic error while programming a quizzer whic hquizzes people on Scrabble three letter words

  1. #1
    Junior Member Jchang504's Avatar
    Join Date
    Nov 2008
    Posts
    28
    Thanks
    0
    Thanked 2 Times in 2 Posts

    Default Logic error while programming a quizzer whic hquizzes people on Scrabble three letter words

    My program is a quizzer which quizzes people on Scrabble three-letter words (which are quite bizarre and there are just less than 1000 of). Everything seems to be working the way I want it to, but it seems like the program gets "stuck" on the first decision. For example, if the first randomly generated word is a valid word, then even if the next one that comes up isn't, the program will say it is (and vice-versa). I have seen this problem before and I think it is an issue of the loop not repeating the command to check the word, but I'm a little confused exactly where the logic error occurs. All help would be greatly appreciated. (Code included below)

    import javax.swing.JOptionPane;
    import java.util.Random;
    import java.io.FileReader;
    import java.io.BufferedReader;
    import java.io.IOException;
    public class Quizzer3
    {
    public static void main(String[] args) throws IOException
       {
       boolean mainEnd = false;
       Object[] options = {"Continue", "Quit"};
       while(!mainEnd)
       {
       int userOption = JOptionPane.showOptionDialog(null, "Welcome to Quizzer3! This program was designed to test your knowledge of official three letter Scrabble words.\nPress continue to proceed to the Quizzer.", "Quizzer3", JOptionPane.YES_NO_OPTION, JOptionPane.INFORMATION_MESSAGE, null, options, options[0]);
       if(userOption == JOptionPane.NO_OPTION)
          {
          int userReply = JOptionPane.showConfirmDialog(null, "Are you sure you want to exit?", "Quit Quizzer3", JOptionPane.YES_NO_OPTION, JOptionPane.WARNING_MESSAGE);
          if(userReply == JOptionPane.YES_OPTION)
             {
             mainEnd = true;
             System.exit(0);
             }
          }
       if(userOption == JOptionPane.YES_OPTION)
          {
          boolean quizEnd = false;
          Random generator = new Random();
          char[] vowels = {'A', 'E', 'I', 'O', 'U', 'Y'};
          char[] consonants = {'B', 'C', 'D', 'F', 'G', 'H', 'J', 'K', 'L', 'M', 'N', 'P', 'Q', 'R', 'S', 'T', 'V', 'W', 'X', 'Y', 'Z'};
          char[] letters = {'n', 'n', 'n'};
          Object[] choices = {"It's good.", "It's phony!"};
          boolean good = false;
          String def;
          FileReader fr = new FileReader("TWL_Dic_3.txt");
          BufferedReader readWord = new BufferedReader(fr);
          while(!quizEnd)
             {
             int letterGroup = generator.nextInt(22);
             if(letterGroup < 6)
             {
             letters[0] = 'c';
             letters[1] = 'v';
             letters[2] = 'c';
             }
             if(letterGroup > 5 && letterGroup < 10)
             {
             letters[0] = 'v';
             letters[1] = 'c';
             letters[2] = 'v';
             }
             if(letterGroup > 9 && letterGroup < 13)
             {
             letters[0] = 'v';
             letters[1] = 'v';
             letters[2] = 'c';
             }
             if(letterGroup > 12 && letterGroup < 16)
             {
             letters[0] = 'c';
             letters[1] = 'v';
             letters[2] = 'v';
             }
             if(letterGroup > 15 && letterGroup < 18)
             {
             letters[0] = 'c';
             letters[1] = 'c';
             letters[2] = 'v';
             }
             if(letterGroup > 17 && letterGroup < 20)
             {
             letters[0] = 'v';
             letters[1] = 'c';
             letters[2] = 'c';
             }
             if(letterGroup == 20)
             {
             letters[0] = 'v';
             letters[1] = 'v';
             letters[2] = 'v';
             }
             if(letterGroup == 21)
             {
             letters[0] = 'c';
             letters[1] = 'c';
             letters[2] = 'c';
             }
             if(letters[0] == 'c')
             letters[0] = consonants[generator.nextInt(21)];
             else if(letters[0] == 'v')
             letters[0] = vowels[generator.nextInt(6)];
             if(letters[1] == 'c')
             letters[1] = consonants[generator.nextInt(21)];
             else if(letters[1] == 'v')
             letters[1] = vowels[generator.nextInt(6)];
             if(letters[2] == 'c')
             letters[2] = consonants[generator.nextInt(21)];
             else if(letters[2] == 'v')
             letters[2] = vowels[generator.nextInt(6)];
             String word = new String(letters);
             int userChoice = JOptionPane.showOptionDialog(null, "Is this a word?\n" + word, "Is " + word + " a word?", JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE, null, choices, choices[1]);
             if(userChoice == JOptionPane.CLOSED_OPTION)
                {
                quizEnd = true;
                }
             String testWord = readWord.readLine();
             while(testWord != null)
                {
                if(testWord.startsWith(word))
                   {
                   good = true;
                   def = testWord.substring(4);
                   break;
                   }
                testWord = readWord.readLine();
                }
             if(userChoice == JOptionPane.YES_OPTION && good == true)
             JOptionPane.showMessageDialog(null, "Correct!\nYES, " + word + " is a word.", "IS a word", JOptionPane.INFORMATION_MESSAGE);
             if(userChoice == JOptionPane.YES_OPTION && good == false)
             JOptionPane.showMessageDialog(null, "Wrong!\nNO, " + word + " is not a word.", "NOT a word", JOptionPane.ERROR_MESSAGE);
             if(userChoice == JOptionPane.NO_OPTION && good == false)
             JOptionPane.showMessageDialog(null, "Correct!\nNO, " + word + " is not a word.", "NOT a word", JOptionPane.INFORMATION_MESSAGE);
             if(userChoice == JOptionPane.NO_OPTION && good == true)
             JOptionPane.showMessageDialog(null, "Wrong!\nYES, " + word + " is a word.", "IS a word", JOptionPane.ERROR_MESSAGE);
             }
          }
       }
       }
    }


  2. #2
    Junior Member Jchang504's Avatar
    Join Date
    Nov 2008
    Posts
    28
    Thanks
    0
    Thanked 2 Times in 2 Posts

    Default Re: Looping program gets "stuck" and doesn't reset variable

    Note: It's probably the loops at the bottom causing the problem - the top is just the random word generator formula and things.

  3. #3
    mmm.. coffee JavaPF's Avatar
    Join Date
    May 2008
    Location
    United Kingdom
    Posts
    3,336
    My Mood
    Mellow
    Thanks
    258
    Thanked 294 Times in 227 Posts
    Blog Entries
    4

    Default Re: Looping program gets "stuck" and doesn't reset variable

    Hey Jchang504,

    I compiled and run the above code which generates a popup box, but as soon as I click Continue, it falls over and dies

    Exception in thread "main" java.io.FileNotFoundException: TWL_Dic_3.txt (The system cannot find the file specified)

    What is the content of TWL_Dic_3.txt?
    Please use [highlight=Java] code [/highlight] tags when posting your code.
    Forum Tip: Add to peoples reputation by clicking the button on their useful posts.

  4. #4

    Default Re: Looping program gets "stuck" and doesn't reset variable

    Hey Jchang504,

    Can you provide us the TWL_Dic_3.txt file?

  5. #5
    Junior Member Jchang504's Avatar
    Join Date
    Nov 2008
    Posts
    28
    Thanks
    0
    Thanked 2 Times in 2 Posts

    Default Re: Looping program gets "stuck" and doesn't reset variable

    Oh, right. I forgot about that. The file is a dictionary file containing a little less than a thousand words and their definitions. It's too big to attach to a post, but it can be found here:

    Three-Letter Word List

    This may be slightly different than the file I have, in that it may be an older version. However for the program's purposes it is usable. You can copy it into wordpad and save it as TWL_Dic_3.

  6. #6
    mmm.. coffee JavaPF's Avatar
    Join Date
    May 2008
    Location
    United Kingdom
    Posts
    3,336
    My Mood
    Mellow
    Thanks
    258
    Thanked 294 Times in 227 Posts
    Blog Entries
    4

    Default Re: Looping program gets "stuck" and doesn't reset variable

    Hey Jchang504.

    I have downloaded the wordlist and compiled the program.

    It all seems to be working fine for me. I am a bit unsure as to what the problem is?

    Could you please give me an example of the problem?
    Please use [highlight=Java] code [/highlight] tags when posting your code.
    Forum Tip: Add to peoples reputation by clicking the button on their useful posts.

  7. #7
    Junior Member Jchang504's Avatar
    Join Date
    Nov 2008
    Posts
    28
    Thanks
    0
    Thanked 2 Times in 2 Posts

    Default Re: Looping program gets "stuck" and doesn't reset variable

    I will attach a set of pictures to help you understand. The first set (appended with (1)) demonstrates the error that occurs when the initial word that is asked is a good word. When you click on "It's Good!" the correct screen comes up. But then the next question displays a phony word, and when you click "It's Phony!" the results screen says you're wrong, it's good. And no matter what all the following words are, the results screen always says the word is good, and that you are right or wrong depending upon the button you clicked. Likewise, for the set of pictures with (2), if the initial word is phony, the program declares all the words following as phony as well. I can't attach the last results screen error, but I think you get the idea. The results screen for the second set declares the word phony when it is actually good.
    Attached Images Attached Images

  8. #8
    Junior Member Jchang504's Avatar
    Join Date
    Nov 2008
    Posts
    28
    Thanks
    0
    Thanked 2 Times in 2 Posts

    Default Re: Looping program gets "stuck" and doesn't reset variable

    Well here's the second results screen error:
    Attached Images Attached Images

  9. #9
    mmm.. coffee JavaPF's Avatar
    Join Date
    May 2008
    Location
    United Kingdom
    Posts
    3,336
    My Mood
    Mellow
    Thanks
    258
    Thanked 294 Times in 227 Posts
    Blog Entries
    4

    Default Re: Looping program gets "stuck" and doesn't reset variable

    I made my own TWL_Dic_3.txt file with only 5, 3 letter words in it but it is giving me combinations which are not in the list.

    I can see the application generating combinations, so why is the word list needed?
    Please use [highlight=Java] code [/highlight] tags when posting your code.
    Forum Tip: Add to peoples reputation by clicking the button on their useful posts.

  10. #10
    Junior Member Jchang504's Avatar
    Join Date
    Nov 2008
    Posts
    28
    Thanks
    0
    Thanked 2 Times in 2 Posts

    Default Re: Looping program gets "stuck" and doesn't reset variable

    You see, the program itself generates random word combinations. The text file contains all the valid three letter words that are acceptable in Scrabble. The text file is used to check the random word combination to see if it's a word. The user is supposed to guess whether it is a word or not and then the program checks and displays the results. However, the program seems to get stuck on a decision about a word (whether it's good or phony) and then say that all the words that are subsequently displayed are good or phony depending upon the first word's status, regardless of what the actual dictionary contains.

  11. #11
    mmm.. coffee JavaPF's Avatar
    Join Date
    May 2008
    Location
    United Kingdom
    Posts
    3,336
    My Mood
    Mellow
    Thanks
    258
    Thanked 294 Times in 227 Posts
    Blog Entries
    4

    Default Re: Looping program gets "stuck" and doesn't reset variable

    Quote Originally Posted by Jchang504 View Post
    You see, the program itself generates random word combinations. The text file contains all the valid three letter words that are acceptable in Scrabble. The text file is used to check the random word combination to see if it's a word. The user is supposed to guess whether it is a word or not and then the program checks and displays the results. However, the program seems to get stuck on a decision about a word (whether it's good or phony) and then say that all the words that are subsequently displayed are good or phony depending upon the first word's status, regardless of what the actual dictionary contains.
    Hello Jchang504.

    Thank you, I fully understand this application now. I will take a look at it as soon as possible and try to offer a solution.
    Please use [highlight=Java] code [/highlight] tags when posting your code.
    Forum Tip: Add to peoples reputation by clicking the button on their useful posts.

  12. #12
    mmm.. coffee JavaPF's Avatar
    Join Date
    May 2008
    Location
    United Kingdom
    Posts
    3,336
    My Mood
    Mellow
    Thanks
    258
    Thanked 294 Times in 227 Posts
    Blog Entries
    4

    Default Re: Looping program gets "stuck" and doesn't reset variable

    Hello,

    I have fixed the problem. The program seems to be working as expected now after I made some changes.

    You can see the changes I made in bold. Please give this code a try and let me know if it works

    import javax.swing.JOptionPane;
    import java.util.Random;
    import java.io.BufferedReader;
    import java.io.IOException;
    [B]import java.io.InputStreamReader;
    import java.io.FileInputStream;[/B]
     
    public class Quizzer3
    {
     
       [B] public static String testWord;[/B]
     
    public static void main(String[] args) throws IOException
       {
       boolean mainEnd = false;
       Object[] options = {"Continue", "Quit"};
       while(!mainEnd)
       {
       int userOption = JOptionPane.showOptionDialog(null, "Welcome to Quizzer3! This program was designed to test your knowledge of official three letter Scrabble words.\nPress continue to proceed to the Quizzer.", "Quizzer3", JOptionPane.YES_NO_OPTION, JOptionPane.INFORMATION_MESSAGE, null, options, options[0]);
       if(userOption == JOptionPane.NO_OPTION)
          {
          int userReply = JOptionPane.showConfirmDialog(null, "Are you sure you want to exit?", "Quit Quizzer3", JOptionPane.YES_NO_OPTION, JOptionPane.WARNING_MESSAGE);
          if(userReply == JOptionPane.YES_OPTION)
             {
             mainEnd = true;
             System.exit(0);
             }
          }
       if(userOption == JOptionPane.YES_OPTION)
          {
          boolean quizEnd = false;
          Random generator = new Random();
          char[] vowels = {'A', 'E', 'I', 'O', 'U', 'Y'};
          char[] consonants = {'B', 'C', 'D', 'F', 'G', 'H', 'J', 'K', 'L', 'M', 'N', 'P', 'Q', 'R', 'S', 'T', 'V', 'W', 'X', 'Y', 'Z'};
          char[] letters = {'n', 'n', 'n'};
          Object[] choices = {"It's good.", "It's phony!"};
          boolean good = false;
     
          while(!quizEnd)
             {
             int letterGroup = generator.nextInt(22);
             if(letterGroup < 6)
             {
             letters[0] = 'c';
             letters[1] = 'v';
             letters[2] = 'c';
             }
             if(letterGroup > 5 && letterGroup < 10)
             {
             letters[0] = 'v';
             letters[1] = 'c';
             letters[2] = 'v';
             }
             if(letterGroup > 9 && letterGroup < 13)
             {
             letters[0] = 'v';
             letters[1] = 'v';
             letters[2] = 'c';
             }
             if(letterGroup > 12 && letterGroup < 16)
             {
             letters[0] = 'c';
             letters[1] = 'v';
             letters[2] = 'v';
             }
             if(letterGroup > 15 && letterGroup < 18)
             {
             letters[0] = 'c';
             letters[1] = 'c';
             letters[2] = 'v';
             }
             if(letterGroup > 17 && letterGroup < 20)
             {
             letters[0] = 'v';
             letters[1] = 'c';
             letters[2] = 'c';
             }
             if(letterGroup == 20)
             {
             letters[0] = 'v';
             letters[1] = 'v';
             letters[2] = 'v';
             }
             if(letterGroup == 21)
             {
             letters[0] = 'c';
             letters[1] = 'c';
             letters[2] = 'c';
             }
             if(letters[0] == 'c')
             letters[0] = consonants[generator.nextInt(21)];
             else if(letters[0] == 'v')
             letters[0] = vowels[generator.nextInt(6)];
             if(letters[1] == 'c')
             letters[1] = consonants[generator.nextInt(21)];
             else if(letters[1] == 'v')
             letters[1] = vowels[generator.nextInt(6)];
             if(letters[2] == 'c')
             letters[2] = consonants[generator.nextInt(21)];
             else if(letters[2] == 'v')
             letters[2] = vowels[generator.nextInt(6)];
             String word = new String(letters);
             int userChoice = JOptionPane.showOptionDialog(null, "Is this a word?\n" + word, "Is " + word + " a word?", JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE, null, choices, choices[1]);
     
             if(userChoice == JOptionPane.CLOSED_OPTION)
                {
                quizEnd = true;
                }
     
             //Word checking here
    [B]         try
             {
                 FileInputStream fr = new FileInputStream("TWL_Dic_3.txt");
                 BufferedReader readWord = new BufferedReader(new InputStreamReader(fr));
             String strLine;
     
             while((strLine = readWord.readLine())!= null)
             {
              //System.out.println(strLine);
              testWord = strLine;
              if(testWord.startsWith(word))
              {
              System.out.println(word + " FOUND IN FILE!");
              good = true;
              break;
              }else
              //System.out.println("NOT FOUND IN FILE");
              good = false;
             }
     
             }catch(Exception e){
              System.out.println(e);
             }[/B]
     
             //System.out.println(word);
     
             if(userChoice == JOptionPane.YES_OPTION && good == true)
             JOptionPane.showMessageDialog(null, "Correct!\nYES, " + word + " is a word.", "IS a word", JOptionPane.INFORMATION_MESSAGE);
             if(userChoice == JOptionPane.YES_OPTION && good == false)
             JOptionPane.showMessageDialog(null, "Wrong!\nNO, " + word + " is not a word.", "NOT a word", JOptionPane.ERROR_MESSAGE);
             if(userChoice == JOptionPane.NO_OPTION && good == false)
             JOptionPane.showMessageDialog(null, "Correct!\nNO, " + word + " is not a word.", "NOT a word", JOptionPane.INFORMATION_MESSAGE);
             if(userChoice == JOptionPane.NO_OPTION && good == true)
             JOptionPane.showMessageDialog(null, "Wrong!\nYES, " + word + " is a word.", "IS a word", JOptionPane.ERROR_MESSAGE);
             }
          }
       }
       }
    }
    Please use [highlight=Java] code [/highlight] tags when posting your code.
    Forum Tip: Add to peoples reputation by clicking the button on their useful posts.

  13. #13
    Junior Member Jchang504's Avatar
    Join Date
    Nov 2008
    Posts
    28
    Thanks
    0
    Thanked 2 Times in 2 Posts

    Default Re: Looping program gets "stuck" and doesn't reset variable

    Thank you so much for the help! I don't fully understand try blocks and stuff yet, so I'm not exactly sure what all this means, but as long as it works, I'm fine with it. I am thinking of adding a definition feature to the program, so if I want to get the definition, (which follows each word in the same line) I could just add:

    good = true;
    [B]def = testWord.substring(4);[/B]
    break;

    Then I will display the definition on the results screen. Does that seem like it will work?

    Also, is the statement System.out.println(e); necessary?

  14. #14
    mmm.. coffee JavaPF's Avatar
    Join Date
    May 2008
    Location
    United Kingdom
    Posts
    3,336
    My Mood
    Mellow
    Thanks
    258
    Thanked 294 Times in 227 Posts
    Blog Entries
    4

    Talking Re: Looping program gets "stuck" and doesn't reset variable

    Quote Originally Posted by Jchang504 View Post
    Thank you so much for the help! I don't fully understand try blocks and stuff yet, so I'm not exactly sure what all this means, but as long as it works, I'm fine with it.
    try/catch blocks are for catching errors. Some java classes require you to catch the errors before you can sussesfully compile the code. This is also known as error handling.

    Everything you need to know about try/catch & error handling here:

    The try Block (The Java™ Tutorials > Essential Classes > Exceptions)


    However, in this example I didn't need to use a try/catch block because you are already throwing the exceptions (IOException) in the main method.
    This is the lazy mans try/catch effectively.

    Quote Originally Posted by Jchang504 View Post
    Also, is the statement System.out.println(e); necessary?
    When you read about error handling you will see what this is for. This basically allows you to print any errors to the console.

    Quote Originally Posted by Jchang504 View Post
    I am thinking of adding a definition feature to the program, so if I want to get the definition, (which follows each word in the same line) I could just add:

    good = true;
    [B]def = testWord.substring(4);[/B]
    break;
    Then I will display the definition on the results screen. Does that seem like it will work?
    Yes that will work perfectly.

    Here is the updated code with the definition feature:

    import javax.swing.JOptionPane;
    import java.util.Random;
    import java.io.FileReader;
    import java.io.BufferedReader;
    import java.io.IOException;
    import java.io.InputStreamReader;
    import java.io.FileInputStream;
     
     
    public class Quizzer3
    {
     
        public static String testWord;
        [B]public static String def;[/B]
     
    public static void main(String[] args) throws IOException
       {
       boolean mainEnd = false;
       Object[] options = {"Continue", "Quit"};
       while(!mainEnd)
       {
       int userOption = JOptionPane.showOptionDialog(null, "Welcome to Quizzer3! This program was designed to test your knowledge of official three letter Scrabble words.\nPress continue to proceed to the Quizzer.", "Quizzer3", JOptionPane.YES_NO_OPTION, JOptionPane.INFORMATION_MESSAGE, null, options, options[0]);
       if(userOption == JOptionPane.NO_OPTION)
          {
          int userReply = JOptionPane.showConfirmDialog(null, "Are you sure you want to exit?", "Quit Quizzer3", JOptionPane.YES_NO_OPTION, JOptionPane.WARNING_MESSAGE);
          if(userReply == JOptionPane.YES_OPTION)
             {
             mainEnd = true;
             System.exit(0);
             }
          }
       if(userOption == JOptionPane.YES_OPTION)
          {
          boolean quizEnd = false;
          Random generator = new Random();
          char[] vowels = {'A', 'E', 'I', 'O', 'U', 'Y'};
          char[] consonants = {'B', 'C', 'D', 'F', 'G', 'H', 'J', 'K', 'L', 'M', 'N', 'P', 'Q', 'R', 'S', 'T', 'V', 'W', 'X', 'Y', 'Z'};
          char[] letters = {'n', 'n', 'n'};
          Object[] choices = {"It's good.", "It's phony!"};
          boolean good = false;
     
          while(!quizEnd)
             {
             int letterGroup = generator.nextInt(22);
             if(letterGroup < 6)
             {
             letters[0] = 'c';
             letters[1] = 'v';
             letters[2] = 'c';
             }
             if(letterGroup > 5 && letterGroup < 10)
             {
             letters[0] = 'v';
             letters[1] = 'c';
             letters[2] = 'v';
             }
             if(letterGroup > 9 && letterGroup < 13)
             {
             letters[0] = 'v';
             letters[1] = 'v';
             letters[2] = 'c';
             }
             if(letterGroup > 12 && letterGroup < 16)
             {
             letters[0] = 'c';
             letters[1] = 'v';
             letters[2] = 'v';
             }
             if(letterGroup > 15 && letterGroup < 18)
             {
             letters[0] = 'c';
             letters[1] = 'c';
             letters[2] = 'v';
             }
             if(letterGroup > 17 && letterGroup < 20)
             {
             letters[0] = 'v';
             letters[1] = 'c';
             letters[2] = 'c';
             }
             if(letterGroup == 20)
             {
             letters[0] = 'v';
             letters[1] = 'v';
             letters[2] = 'v';
             }
             if(letterGroup == 21)
             {
             letters[0] = 'c';
             letters[1] = 'c';
             letters[2] = 'c';
             }
             if(letters[0] == 'c')
             letters[0] = consonants[generator.nextInt(21)];
             else if(letters[0] == 'v')
             letters[0] = vowels[generator.nextInt(6)];
             if(letters[1] == 'c')
             letters[1] = consonants[generator.nextInt(21)];
             else if(letters[1] == 'v')
             letters[1] = vowels[generator.nextInt(6)];
             if(letters[2] == 'c')
             letters[2] = consonants[generator.nextInt(21)];
             else if(letters[2] == 'v')
             letters[2] = vowels[generator.nextInt(6)];
             String word = new String(letters);
             int userChoice = JOptionPane.showOptionDialog(null, "Is this a word?\n" + word, "Is " + word + " a word?", JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE, null, choices, choices[1]);
     
             if(userChoice == JOptionPane.CLOSED_OPTION)
                {
                quizEnd = true;
                }
     
             //Word checking here
             try
             {
                 FileInputStream fr = new FileInputStream("TWL_Dic_3.txt");
                 BufferedReader readWord = new BufferedReader(new InputStreamReader(fr));
             String strLine;
     
             while((strLine = readWord.readLine())!= null)
             {
              //System.out.println(strLine);
              testWord = strLine;
              if(testWord.startsWith(word))
              {
              System.out.println(word + " FOUND IN FILE!");
              good = true;
              [B]def = testWord.substring(4);
              System.out.println(def);[/B]
              break;
              }else
              //System.out.println("NOT FOUND IN FILE");
              good = false;
             }
     
             }catch(Exception e){
              System.out.println(e);
             }
     
             //System.out.println(word);
     
             if(userChoice == JOptionPane.YES_OPTION && good == true)
             JOptionPane.showMessageDialog(null, "Correct!\nYES, " + word + " is a word.", "IS a word", JOptionPane.INFORMATION_MESSAGE);
             if(userChoice == JOptionPane.YES_OPTION && good == false)
             JOptionPane.showMessageDialog(null, "Wrong!\nNO, " + word + " is not a word.", "NOT a word", JOptionPane.ERROR_MESSAGE);
             if(userChoice == JOptionPane.NO_OPTION && good == false)
             JOptionPane.showMessageDialog(null, "Correct!\nNO, " + word + " is not a word.", "NOT a word", JOptionPane.INFORMATION_MESSAGE);
             if(userChoice == JOptionPane.NO_OPTION && good == true)
             JOptionPane.showMessageDialog(null, "Wrong!\nYES, " + word + " is a word.", "IS a word", JOptionPane.ERROR_MESSAGE);
             }
          }
       }
       }
    }
    Please use [highlight=Java] code [/highlight] tags when posting your code.
    Forum Tip: Add to peoples reputation by clicking the button on their useful posts.

  15. #15
    Junior Member Jchang504's Avatar
    Join Date
    Nov 2008
    Posts
    28
    Thanks
    0
    Thanked 2 Times in 2 Posts

    Default Re: Looping program gets "stuck" and doesn't reset variable

    What I meant when I asked if the command to print the error was necessary was, will omitting it impair the program's function in any way? My understanding is that this command is used to display the cause of the error so that the programmer can understand it/troubleshoot the problem. I am going to package this application to a .jar file, so what would happen to this command which displays the message in command prompt if I executed the .jar file? Would it automatically open up a command prompt window to display the message? If it won't cause any problems with the .jar file, then I will be glad to leave it in and just package the application with the current code.

  16. #16
    mmm.. coffee JavaPF's Avatar
    Join Date
    May 2008
    Location
    United Kingdom
    Posts
    3,336
    My Mood
    Mellow
    Thanks
    258
    Thanked 294 Times in 227 Posts
    Blog Entries
    4

    Talking Re: Looping program gets "stuck" and doesn't reset variable

    Quote Originally Posted by Jchang504 View Post
    What I meant when I asked if the command to print the error was necessary was, will omitting it impair the program's function in any way? My understanding is that this command is used to display the cause of the error so that the programmer can understand it/troubleshoot the problem. I am going to package this application to a .jar file, so what would happen to this command which displays the message in command prompt if I executed the .jar file? Would it automatically open up a command prompt window to display the message? If it won't cause any problems with the .jar file, then I will be glad to leave it in and just package the application with the current code.
    You do not have to put anything in the catch block if you don't want to. This will not effect the running of the application. Like you say, the System.out.println(e); is so the programmer can see a better description of the problem when an error occurs.

    Will you be running the .jar through a command prompt? As far as im aware, the String will be printed to the console in the original command window. It won't cause any problems.

    You could easily edit that code so the description is included in the popup box. I think that would be better use of the description function. I had it printing to the console as an example...
    Please use [highlight=Java] code [/highlight] tags when posting your code.
    Forum Tip: Add to peoples reputation by clicking the button on their useful posts.

  17. #17
    Junior Member Jchang504's Avatar
    Join Date
    Nov 2008
    Posts
    28
    Thanks
    0
    Thanked 2 Times in 2 Posts

    Default Re: Looping program gets "stuck" and doesn't reset variable

    Yes, I did change the System.out.println() statements so that I wouldn't need the console window. I like to run .jar applications from the desktop. Thanks for clearing that up. I will now mark this thread as solved. But as another question, is there any way to randomly select one of the words (lines) from the dictionary file? Or do I have to put them into an array in order to select them randomly using something like this:

    String[] words = {"AAH", "AAL", "AAS", ...};
    Random generator = new Random;
    int number = generator.nextInt(numOfWords); //numOfWords I don't know exactly; has to be determined
    System.out.println(words[number]);

    Is this the only way I could do it or could I somehow select a line randomly from the text file without needing to create an array?

  18. #18
    mmm.. coffee JavaPF's Avatar
    Join Date
    May 2008
    Location
    United Kingdom
    Posts
    3,336
    My Mood
    Mellow
    Thanks
    258
    Thanked 294 Times in 227 Posts
    Blog Entries
    4

    Default Re: [SOLVED] Looping program gets stuck and doesn't reset variable

    So you just want to get a line randomly from the text file? What is this for?
    Please use [highlight=Java] code [/highlight] tags when posting your code.
    Forum Tip: Add to peoples reputation by clicking the button on their useful posts.

  19. #19
    Junior Member Jchang504's Avatar
    Join Date
    Nov 2008
    Posts
    28
    Thanks
    0
    Thanked 2 Times in 2 Posts

    Default Re: [SOLVED] Looping program gets stuck and doesn't reset variable

    Well I realize that my program generates good words only about 25% of the time, and I want that number to be something more like 50%. I thought maybe I could use a Random to select a word from the list (which are all good) maybe 50% of the time, and then the other 50% it would use the random generator algorithm, which would add (according to math) another 12.5%. Therefore, it would produce good words 62.5%, making it more useful so that you're not just clicking Phony repeatedly until finally a good word appears. So in order to do this, I need to be able to select a random word from the text file. (I apologize for the confusing math, I'll include a diagram.)


    _________________________> (if == 0) Select word from list (100% chance of good)
    randomGenerator.nextInt(2)________________________ _________________________> Display word (62.5% chance of good - average of 100% and 25%)
    _________________________> (if == 1) Use word algorithm (25% chance of good)


    I apologize if my diagram is even more confusing. The extra spaces were getting deleted so I had to use underscores.

  20. #20
    mmm.. coffee JavaPF's Avatar
    Join Date
    May 2008
    Location
    United Kingdom
    Posts
    3,336
    My Mood
    Mellow
    Thanks
    258
    Thanked 294 Times in 227 Posts
    Blog Entries
    4

    Default Re: [SOLVED] Looping program gets stuck and doesn't reset variable

    I have put this quick example together. It will randomly grab a line from your wordlist and print to the console.

    import java.io.BufferedReader;
    import java.io.InputStreamReader;
    import java.io.FileInputStream;
    import java.util.*;
     
    public class RandomLine {
     
        public static int numberOfLines;
        public static String randomLine;
     
        public static void main(String[] args) throws Exception {
     
            Random random = new Random();
     
            FileInputStream fr = new FileInputStream("TWL_Dic_3.txt");
            BufferedReader readWord = new BufferedReader(new InputStreamReader(fr));
            String strLine;
     
            // Tells us how many lines are in the file.
            while ((strLine = readWord.readLine()) != null) {
                numberOfLines++;
            }
     
            //Generate random number upto the numberOfLines value
            int myRand = random.nextInt(numberOfLines);
     
            FileInputStream fr2 = new FileInputStream("TWL_Dic_3.txt");
            BufferedReader readWord2 = new BufferedReader(new InputStreamReader(fr2));
            String strLine2 = "";
     
            //Loop through to the random line number
            for(int a = 0; a < myRand; a++){
                randomLine = readWord2.readLine();
            }
     
            //System.out.println(numberOfLines);
            //System.out.println(myRand);
            System.out.println("Random Word: " + randomLine);
        }
     
    }
    I am now away over the Easter Holidays but I will try to reply whenever possible.

    Best of luck.
    Please use [highlight=Java] code [/highlight] tags when posting your code.
    Forum Tip: Add to peoples reputation by clicking the button on their useful posts.

  21. #21
    Junior Member Jchang504's Avatar
    Join Date
    Nov 2008
    Posts
    28
    Thanks
    0
    Thanked 2 Times in 2 Posts

    Default Re: [SOLVED] Looping program gets stuck and doesn't reset variable

    Thanks, this is an enormous help. I could just run this program once, removing "//" from the print statements, and then i would know how many lines there are and I wouldn't need the first half or so of the program, I could just use the preset number (which doesn't change) for the random generator, correct?

  22. #22
    mmm.. coffee JavaPF's Avatar
    Join Date
    May 2008
    Location
    United Kingdom
    Posts
    3,336
    My Mood
    Mellow
    Thanks
    258
    Thanked 294 Times in 227 Posts
    Blog Entries
    4

    Default Re: [SOLVED] Looping program gets stuck and doesn't reset variable

    Quote Originally Posted by Jchang504 View Post
    Thanks, this is an enormous help. I could just run this program once, removing "//" from the print statements, and then i would know how many lines there are and I wouldn't need the first half or so of the program, I could just use the preset number (which doesn't change) for the random generator, correct?
    I think you would need to incorporate most of that code. See what you can do and post your updated code if you get stuck..
    Please use [highlight=Java] code [/highlight] tags when posting your code.
    Forum Tip: Add to peoples reputation by clicking the button on their useful posts.

  23. #23
    mmm.. coffee JavaPF's Avatar
    Join Date
    May 2008
    Location
    United Kingdom
    Posts
    3,336
    My Mood
    Mellow
    Thanks
    258
    Thanked 294 Times in 227 Posts
    Blog Entries
    4

    Default Re: [SOLVED] Looping program gets stuck and doesn't reset variable

    Hey Jchang504,

    Did you manage to incorporate the new code OK?
    Please use [highlight=Java] code [/highlight] tags when posting your code.
    Forum Tip: Add to peoples reputation by clicking the button on their useful posts.

  24. #24
    Junior Member Jchang504's Avatar
    Join Date
    Nov 2008
    Posts
    28
    Thanks
    0
    Thanked 2 Times in 2 Posts

    Default Re: [SOLVED] Looping program gets stuck and doesn't reset variable

    Actually, by editing the ratio of the word patterns (ex. Consonant-Vowel-Consonant, Vowel-Consonant-Vowel, etc.) I was able to improve the ratio of valid words to above 50%, so I didn't think it was necessary to incorporate the random word-pulling from the list. However, that code I'm sure will come in handy with my next project that I'm working on. Thanks.

  25. #25
    mmm.. coffee JavaPF's Avatar
    Join Date
    May 2008
    Location
    United Kingdom
    Posts
    3,336
    My Mood
    Mellow
    Thanks
    258
    Thanked 294 Times in 227 Posts
    Blog Entries
    4

    Default Re: [SOLVED] Looping program gets stuck and doesn't reset variable

    Quote Originally Posted by Jchang504 View Post
    Actually, by editing the ratio of the word patterns (ex. Consonant-Vowel-Consonant, Vowel-Consonant-Vowel, etc.) I was able to improve the ratio of valid words to above 50%, so I didn't think it was necessary to incorporate the random word-pulling from the list. However, that code I'm sure will come in handy with my next project that I'm working on. Thanks.
    Brilliant. I am glad you found another solution.
    Please use [highlight=Java] code [/highlight] tags when posting your code.
    Forum Tip: Add to peoples reputation by clicking the button on their useful posts.

Similar Threads

  1. Problem with reset on JComboBox
    By WorkingMan in forum AWT / Java Swing
    Replies: 4
    Last Post: April 25th, 2013, 12:19 PM
  2. Problem while programming a simple game of Car moving on a road
    By rojroj in forum Java Theory & Questions
    Replies: 3
    Last Post: April 2nd, 2009, 10:24 AM
  3. How to do thread communication in java
    By Koren3 in forum Threads
    Replies: 4
    Last Post: March 29th, 2009, 10:49 AM
  4. stuck on this program can any one help me
    By clive in forum AWT / Java Swing
    Replies: 2
    Last Post: March 10th, 2009, 05:54 PM

Tags for this Thread