Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

read specific line from text file

 
Lesole Mphinyane
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
  • 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..
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 65111
89
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • 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
 
Rob Camick
Ranch Hand
Posts: 2612
9
  • Mark post as helpful
  • send pies
  • 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
  • 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
Ranch Hand
Posts: 2612
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Create a loop and read 4 lines.
 
Lesole Mphinyane
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
  • 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
Ranch Hand
Posts: 2612
9
  • Mark post as helpful
  • send pies
  • 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
  • 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
Ranch Hand
Posts: 2612
9
  • Mark post as helpful
  • send pies
  • 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
  • Quote
  • Report post to moderator
is the anyhow i can do it without using ArrayLists?
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 65111
89
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • 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
  • 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
Ranch Hand
Posts: 2612
9
  • Mark post as helpful
  • send pies
  • 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.

 
Rob Spoor
Sheriff
Pie
Posts: 20606
60
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • 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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic