• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

LineNumberReader

 
Ranch Hand
Posts: 167
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi
I have a text file which has 30 lines. I would like to generate a random number between 0 and 30, then i want to read only this line from the file when a button is clicked. Here is the code:


But it reads always only the first line of the file although i've set the Line with . br.setLineNumber(6);even if i change the line number.
br.getLineNumber() is only to check which line is readed. It gives (for example) 6 but the string is the first line of the text. How does it make sense for the LineNumberReader class to have the setLineNumber(int LineNumber) method when i can not read this line? What is my mistake?
How can i read randomly only a line from a text file?
Should i use another class?
Should i stay or should i go?
 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Excerpt from the Java Docs... This class does not do what you think it does.

By default, line numbering begins at 0. This number increments as data is read, and can be changed with a call to setLineNumber(int). Note however, that setLineNumber(int) does not actually change the current position in the stream; it only changes the value that will be returned by getLineNumber().

 
Kudret Serin
Ranch Hand
Posts: 167
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for your reply. I have read it, therefore i ask under what conditions can this setLineNumber(int LineNumber) method be used? Who uses it for what?
By the way, I have solved my problem by reading the lines from file into an ArrayList. I get the sentences randomly from this list. Is there a better way?
Cheers
 
Ranch Hand
Posts: 637
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You might want to have a properties file with keys being 1, 2, 3, etc, and fetch only one value based on the key. This seems simpler than loading the whole data (what if you have 100 lines.)

 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think maybe Stuart doesn't understand the issue.

Imagine that you have written a compiler which reads a source file line by line. It of course needs to use the line number to report errors, so perhaps a LineNumberReader would be helpful.

Now imagine you add a preprocessor which modifies the source code before the compiler sees it (Java doesn't have such a thing, but C and C++ do.) That preprocessor might actually add lines to the source code. But your compiler needs to report the original line numbers, so you can't use LineNumberReader anymore, can you?

Sure you can. You add an instruction to your compiler, a special signal, which says "pretend this is line number 10". Then when the compiler sees this, even if it's read 100 lines, it knows that from the user's perspective, we're still on line 10. That's what setLineNumber() is for.

C preprocessor/compiler setups usually use a line something like

#pragma line 10

to do exactly what I've described here.
 
Stuart Ash
Ranch Hand
Posts: 637
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sorry, I assumed that the coder had control over the format of the text to be read. So I proposed an alternate way of doing it.

I really wonder why setLineNumber doesn't work as expected.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic