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(LineRead er.getLine Number()) + 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?
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
SecretWord = LineReader.readLine();
g.drawString (Integer.toString(LineRead
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?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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;
}
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
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_VAL UE)
//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.ne xtLong())> fileSize)
;
}
inputFile.seek(offset);
String SecretWord=inputFile.readL ine(); //This one is probably just a fragment of a word
SecretWord=inputFile.readL ine();
inputFile.close();
g.drawString (Long.toString(offset) + SecretWord + "TEST", 40, 40);
return;
}
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,
long fileSize = wordFile.length();
long offset;
if (fileSize<=Integer.MAX_VAL
//Use this if the file is small because it will give
//you a better random number
offset=Generator.nextInt((
else {
while((offset=Generator.ne
;
}
inputFile.seek(offset);
String SecretWord=inputFile.readL
SecretWord=inputFile.readL
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.readL ine();
But since offset *can* be fileSize-1
SecretWord could be null after
>> SecretWord=inputFile.readL
So, some extra code is needed to overcome that situation
Thanks for accepting
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?
List theWords = new ArrayList();
How do I fix this problem though?
You want java.util.List!
ASKER
K, thanks
It does not change the position in the file.