We're a community of 1076K IT Pros here for help, advice, solutions, professional growth and fun. Join us!
1,075,839 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Start New Discussion Reply to this Discussion

reading certain line of a text file

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();
5
Contributors
10
Replies
2 Days
Discussion Span
3 Months Ago
Last Updated
11
Views
yup790
Light Poster
36 posts since Dec 2009
Reputation Points: 9
Solved Threads: 0
Skill Endorsements: 0
stultuske
Industrious Poster
4,371 posts since Jan 2007
Reputation Points: 1,318
Solved Threads: 610
Skill Endorsements: 24

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

jalpesh_007
Posting Whiz
336 posts since Sep 2010
Reputation Points: 4
Solved Threads: 36
Skill Endorsements: 4

it only reads the first line.

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

yup790
Light Poster
36 posts since Dec 2009
Reputation Points: 9
Solved Threads: 0
Skill Endorsements: 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.

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

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.

bguild
Posting Whiz
330 posts since Oct 2012
Reputation Points: 166
Solved Threads: 73
Skill Endorsements: 7

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

yup790
Light Poster
36 posts since Dec 2009
Reputation Points: 9
Solved Threads: 0
Skill Endorsements: 0

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

In what way

yup790
Light Poster
36 posts since Dec 2009
Reputation Points: 9
Solved Threads: 0
Skill Endorsements: 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.

stultuske
Industrious Poster
4,371 posts since Jan 2007
Reputation Points: 1,318
Solved Threads: 610
Skill Endorsements: 24

This article has been dead for over three months: Start a new discussion instead

Post: Markdown Syntax: Formatting Help
 
You
 
© 2013 DaniWeb® LLC
Page rendered in 0.0965 seconds using 2.81MB