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"));
                        word = r.readLine();
4 Years
Discussion Span
Last Post by stultuske
Featured Replies
  • 3

    when in doubt, google :) [read file line by line](http://stackoverflow.com/questions/5868369/how-to-read-a-large-text-file-line-by-line-in-java) Read More


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....


it only reads the first line.

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


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();

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.

Edited by Lucaci Andrew


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.


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.


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


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.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.