stultuske
Industrious Poster
4,371 posts since Jan 2007
Reputation Points: 1,318
Solved Threads: 610
Skill Endorsements: 24
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.
Lucaci Andrew
Practically a Master Poster
649 posts since Jan 2012
Reputation Points: 91
Solved Threads: 91
Skill Endorsements: 12
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.
stultuske
Industrious Poster
4,371 posts since Jan 2007
Reputation Points: 1,318
Solved Threads: 610
Skill Endorsements: 24
no. but it could be you implemented the code wrong.
stultuske
Industrious Poster
4,371 posts since Jan 2007
Reputation Points: 1,318
Solved Threads: 610
Skill Endorsements: 24
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.
stultuske
Industrious Poster
4,371 posts since Jan 2007
Reputation Points: 1,318
Solved Threads: 610
Skill Endorsements: 24