It seems clear that yup790 has the mistaken impression that ... should make it possible to move the reader to the start of any given line. When yup790 says ... It means reading the entire file and then calling ... in the hope that reading through the file has somehow made the line accessible to ... . But ... doesn't do anything like what yup790 hopes it does. Ironically, the javadoc for ... even warns…
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.
int word_chooser = (int) (Math.random()*300);
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.
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.
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.