[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

LineNumberReader.setLineNumber() doesn't work??

Posted on 2004-11-22
10
Medium Priority
?
1,422 Views
Last Modified: 2008-03-17
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?
0
Comment
Question by:UltimateApocalypse
  • 5
  • 2
  • 2
  • +1
10 Comments
 
LVL 92

Expert Comment

by:objects
ID: 12652150
thats not how setLineNumberWorks(), it simply assignes the number for the current line.
It does not change the position in the file.
0
 
LVL 37

Accepted Solution

by:
zzynx earned 200 total points
ID: 12652484
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
 
LVL 37

Expert Comment

by:zzynx
ID: 12652503
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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 21

Expert Comment

by:MogalManic
ID: 12653499
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
 
LVL 37

Expert Comment

by:zzynx
ID: 12653857
Not a bad idea.
But since offset *can* be fileSize-1
SecretWord could be null after
>> SecretWord=inputFile.readLine();

0
 
LVL 37

Expert Comment

by:zzynx
ID: 12653870
So, some extra code is needed to overcome that situation
0
 
LVL 37

Expert Comment

by:zzynx
ID: 12656133
Thanks for accepting
0
 

Author Comment

by:UltimateApocalypse
ID: 12656153
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
 
LVL 21

Expert Comment

by:MogalManic
ID: 12656209
You want java.util.List!
0
 

Author Comment

by:UltimateApocalypse
ID: 12656229
K, thanks
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
This video teaches viewers about errors in exception handling.
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.
Suggested Courses
Course of the Month19 days, 18 hours left to enroll

873 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question