1,105,371 Community Members

reading certain line of a text file

Member Avatar
yup790
Light Poster
36 posts since Dec 2009
Reputation Points: -1 [?]
Q&As Helped to Solve: 1 [?]
Skill Endorsements: 0 [?]
 
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();
Member Avatar
stultuske
Posting Expert
5,290 posts since Jan 2007
Reputation Points: 935 [?]
Q&As Helped to Solve: 774 [?]
Skill Endorsements: 35 [?]
Featured
 
3
 

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

Member Avatar
jalpesh_007
Posting Whiz
385 posts since Sep 2010
Reputation Points: -3 [?]
Q&As Helped to Solve: 43 [?]
Skill Endorsements: 6 [?]
 
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....

Member Avatar
yup790
Light Poster
36 posts since Dec 2009
Reputation Points: -1 [?]
Q&As Helped to Solve: 1 [?]
Skill Endorsements: 0 [?]
 
0
 

it only reads the first line.

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

Member Avatar
Lucaci Andrew
Master Poster
746 posts since Jan 2012
Reputation Points: 128 [?]
Q&As Helped to Solve: 116 [?]
Skill Endorsements: 18 [?]
 
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.

Member Avatar
stultuske
Posting Expert
5,290 posts since Jan 2007
Reputation Points: 935 [?]
Q&As Helped to Solve: 774 [?]
Skill Endorsements: 35 [?]
Featured
 
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.

Member Avatar
bguild
Posting Whiz
339 posts since Oct 2012
Reputation Points: 163 [?]
Q&As Helped to Solve: 84 [?]
Skill Endorsements: 8 [?]
 
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.

Member Avatar
yup790
Light Poster
36 posts since Dec 2009
Reputation Points: -1 [?]
Q&As Helped to Solve: 1 [?]
Skill Endorsements: 0 [?]
 
0
 

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

Member Avatar
stultuske
Posting Expert
5,290 posts since Jan 2007
Reputation Points: 935 [?]
Q&As Helped to Solve: 774 [?]
Skill Endorsements: 35 [?]
Featured
 
0
 

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

Member Avatar
yup790
Light Poster
36 posts since Dec 2009
Reputation Points: -1 [?]
Q&As Helped to Solve: 1 [?]
Skill Endorsements: 0 [?]
 
0
 

In what way

Member Avatar
stultuske
Posting Expert
5,290 posts since Jan 2007
Reputation Points: 935 [?]
Q&As Helped to Solve: 774 [?]
Skill Endorsements: 35 [?]
Featured
 
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 three months: Start a new discussion instead
Post:
Start New Discussion
Tags Related to this Article