1.11M Members

reading certain line of a text file

 
0
 

I have tried looping through the text file but all it does is read the first line.

I tried linefilereader but it still only reads the first line.

int word_chooser = (int) (Math.random()*300);


                    LineNumberReader r = new LineNumberReader(new FileReader("words.txt"));
                        r.setLineNumber(word_chooser);
                        word = r.readLine();
 
3
 

when in doubt, google :)
read file line by line

 
0
 

You are asking about 2 different operations here.
You want to know how to:

Read a file from disk into a group of strings.
Randomly choose 1 string from a group of strings.

you have to use bufferedreader and list for reading any no. of line.
First read file using bufferreader and store every line into list one by one.
then generate random number and read that no. line from list.

that's it....

 
0
 

it only reads the first line.

In response to stultuske I tried looping through first and that doesn't work.

 
0
 

How about looping like this:

int word_chooser = (int) (Math.random()*300);
String word;
LineNumberReader r = new LineNumberReader(new FileReader("words.txt"));
while (r.getLineNumber()<word_chooser) r.readLine();
word = r.readLine();
System.out.println(word);

It's rather impossible to read just a line from the middle of the file not knowing anything about it from advance. You'll have to read "word_chooser" lines before you get to your desired one. Line numbering starts from 0.

 
0
 

yup790: saying - I tried looping through doesn't tell us much. show us how you tried, then maybe we can see where you went wrong.

I can assure you, the code in that link, if applied correctly (and if your .txt file has more than one line in it) will work.

 
1
 

It seems clear that yup790 has the mistaken impression that java.io.LineNumberReader.setLineNumber should make it possible to move the reader to the start of any given line. When yup790 says

I tried looping through first and that doesn't work.

It means reading the entire file and then calling setLineNumber in the hope that reading through the file has somehow made the line accessible to setLineNumber. But setLineNumber doesn't do anything like what yup790 hopes it does. Ironically, the javadoc for LineNumberReader even warns about exactly this mistake, but only in the overall class documentation at the top; the documentation for the method is ambiguous.

Just to make it perfectly clear to everyone, setLineNumber doesn't change which line is the current line. It only changes the number of the current line. For example, you can't return to the beginning of the file by calling setLineNumber(0); all it will do is cause the current line to be numbered 0 as if it were the beginning of the file.

 
0
 

Could it be that the text file needs configuring:
ie \n and the end of lines?

 
0
 

no. but it could be you implemented the code wrong.

 
0
 

In what way

 
0
 

do you really want me to go blind guessing without seeing any of your code?
there are tons of examples of code for this functionality online, I'm pretty sure there even are some in the "code snippets" section of this forum.
not to mention that every decent java textbook contains an example for this.
if you can't show us your code for some reason, you may want to go and search for working examples.

You
This article has been dead for over six months: Start a new discussion instead
Post:
Start New Discussion
Tags Related to this Article