LineNumberReader.setLineNumber() doesn't work??

I have following method to generate a random word from a list of words stored in a TXT file

public void GenerateWord (Graphics g) throws Exception
{
      int MaxLine = 0;

      FileReader InputFile = new FileReader("WordList.txt");
      LineNumberReader LineReader = new LineNumberReader (InputFile);

      SecretWord = LineReader.readLine();
      while (SecretWord != null)
      {
            SecretWord = LineReader.readLine();
            ++MaxLine;
      }

      Random Generator = new Random();

      LineReader.setLineNumber (Generator.nextInt(MaxLine - 1) + 1);
      SecretWord = LineReader.readLine();

      g.drawString (Integer.toString(LineReader.getLineNumber()) + SecretWord + "TEST", 40, 40);

      return;
}

After I have determined a max number of lines in the TXT file, I use setLineNumber () to set the current line and then read it.
However, it seems to me that setLineNumber() did not work because whatever I do, the SecretWord is always a NULL.
Is something worng?
UltimateApocalypseAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

objectsCommented:
thats not how setLineNumberWorks(), it simply assignes the number for the current line.
It does not change the position in the file.
0
zzynxSoftware engineerCommented:
You'd better store the read words in a List and afterwards pick one out of the list:

public void GenerateWord (Graphics g) throws Exception
{
     List theWords = new ArrayList();

     int MaxLine = 0;

     FileReader InputFile = new FileReader("WordList.txt");
     LineNumberReader LineReader = new LineNumberReader (InputFile);

     SecretWord = LineReader.readLine();
     while (SecretWord != null)
     {
          SecretWord = LineReader.readLine();
          theWords.add(SecretWord);
          ++MaxLine;
     }

     Random Generator = new Random();
     int index = Generator.nextInt(MaxLine);  // 0...MaxLine-1
     SecretWord = (String) theWords.get( index );

     g.drawString (index + " " + SecretWord + "TEST", 40, 40);

     return;
}
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
zzynxSoftware engineerCommented:
You don't even need the MaxLine variable (nor the LineNumberReader):

public void GenerateWord (Graphics g) throws Exception
{
     List theWords = new ArrayList();

     FileReader InputFile = new FileReader("WordList.txt");
     BufferedReader reader = new BufferedReader(InputFile);
     while ( (SecretWord = reader.readLine())!= null)
     {
          theWords.add(SecretWord);
     }

     Random Generator = new Random();
     int index = Generator.nextInt(theWords.size());  // 0...size-1
     SecretWord = (String) theWords.get( index );

     g.drawString (index + " " + SecretWord + "TEST", 40, 40);

     return;
}
0
Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

MogalManicCommented:
If the wordlist is large and/or you don't want to read in the entire list, you could try something like this:
      public void GenerateWord (Graphics g) throws Exception
      {
          int MaxLine = 0;

           Random Generator = new Random();
                     final File wordFile = new File("WordList.txt");
           RandomAccessFile inputFile=new RandomAccessFile(wordFile,"r");
           long fileSize = wordFile.length();
           long offset;
           if (fileSize<=Integer.MAX_VALUE)
               //Use this if the file is small because it will give
               //you a better random number
               offset=Generator.nextInt((int) fileSize);
                     else {
               while((offset=Generator.nextLong())>fileSize)
                    ;
           }
          inputFile.seek(offset);
          String SecretWord=inputFile.readLine();  //This one is probably just a fragment of a word
          SecretWord=inputFile.readLine();
            
          inputFile.close();
            
          g.drawString (Long.toString(offset) + SecretWord + "TEST", 40, 40);

          return;
      }
0
zzynxSoftware engineerCommented:
Not a bad idea.
But since offset *can* be fileSize-1
SecretWord could be null after
>> SecretWord=inputFile.readLine();

0
zzynxSoftware engineerCommented:
So, some extra code is needed to overcome that situation
0
zzynxSoftware engineerCommented:
Thanks for accepting
0
UltimateApocalypseAuthor Commented:
reference to List is ambiguous, both class java.util.List in java.util and class java.awt.List in java.awt match
List theWords = new ArrayList();

How do I fix this problem though?
0
MogalManicCommented:
You want java.util.List!
0
UltimateApocalypseAuthor Commented:
K, thanks
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.