Link to home
Start Free TrialLog in
Avatar of UltimateApocalypse
UltimateApocalypse

asked on

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?
Avatar of Mick Barry
Mick Barry
Flag of Australia image

thats not how setLineNumberWorks(), it simply assignes the number for the current line.
It does not change the position in the file.
ASKER CERTIFIED SOLUTION
Avatar of zzynx
zzynx
Flag of Belgium image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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;
}
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;
      }
Not a bad idea.
But since offset *can* be fileSize-1
SecretWord could be null after
>> SecretWord=inputFile.readLine();

So, some extra code is needed to overcome that situation
Thanks for accepting
Avatar of UltimateApocalypse
UltimateApocalypse

ASKER

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?
You want java.util.List!
K, thanks