• 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

read specific line from text file

 
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hey all, look at my code hear;


how do i read text file line at num,
num is the random number generated...
pliz help..
 
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Lesole Mphinyane wrote:pliz help..


Please use real words when posting to the forums. Abbreviations such as "pliz" in place of "please" only serve to make your posts more difficult to read and less likely to generate useful responses.

Please read this for more information.

thanks,
bear
JavaRanch sheriff
 
Rancher
Posts: 3324
32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You can't randomly read a line from a file (unless each line in the file is of the same length, then you can use a RandomAccesFile), you need to read every line sequentially.

So you could read each line of the file into an ArrayList. Then when you generated the random number you access the line from the ArrayList.
 
Lesole Mphinyane
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks Bear..
@Rob, um not quite familiar with ArrayLists,
given a text file of 10 lines, how do i read line number 4..?
 
Rob Camick
Rancher
Posts: 3324
32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Create a loop and read 4 lines.
 
Lesole Mphinyane
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Rob Camick wrote:Create a loop and read 4 lines.


i mean reading the fourth line only, is it possible
 
Rob Camick
Rancher
Posts: 3324
32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm not going to repeat myself, I already gave you the answer to that question!
 
Lesole Mphinyane
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Rob Camick wrote:I'm not going to repeat myself, I already gave you the answer to that question!


thanks but i didn't get it clearly.
 
Rob Camick
Rancher
Posts: 3324
32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

thanks but i didn't get it clearly.



What did I say that is so confusing?
 
Lesole Mphinyane
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
is the anyhow i can do it without using ArrayLists?
 
Bear Bibeault
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sure. You could use an array. But that would likely be wasteful and inefficient and prone to error unless you know in advance how many lines to read.

What's your beef with ArrayLists?
 
Lesole Mphinyane
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Bear Bibeault wrote:Sure. You could use an array. But that would likely be wasteful and inefficient and prone to error unless you know in advance how many lines to read.

What's your beef with ArrayLists?



its not that i have beef with ArrayLists, i cant learn them now.. will try to use the LineNumberReader. how is it
 
Rob Camick
Rancher
Posts: 3324
32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Lesole Mphinyane wrote:
its not that i have beef with ArrayLists, i cant learn them now..



What have you been doing for the last hour. It only takes 5 minutes to learn how to use them.

Or you could continue to waste time by trying to use a LineNumberReader, which won't work either.

 
Sheriff
Posts: 22781
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You can only read a specific line without using a List if you already know how many lines there are. If you don't you may end up trying to read a line that simply isn't there.

As for how List and ArrayList work, it's really simple. In pseudo code:
For more information, read this.
 
reply
    Bookmark Topic Watch Topic
  • New Topic