How to output random line from a file

Reply

Join Date: May 2007
Posts: 20
Reputation: Lioshenka is an unknown quantity at this point 
Solved Threads: 0
Lioshenka Lioshenka is offline Offline
Newbie Poster

How to output random line from a file

 
0
  #1
Feb 22nd, 2008
Hi there!!!
I am back with more problems, now in Java

What I am trying to do is to make an application to read and output a random line from a text file, but everything I know how to do is to output the whole lot. How can I access and output a RANDOM line from a file???

My current class is as follows:

  1. package Talking;
  2.  
  3. import java.io.*;
  4.  
  5. public class Talking{
  6. public static void main(String[] args) throws IOException{
  7. File f;
  8. f=new File("Talking.txt");
  9.  
  10. if(!f.exists()&& f.length()<0)
  11. System.out.println("The specified file does not exist");
  12.  
  13. else{
  14. FileInputStream finp=new FileInputStream(f);
  15. byte b;
  16. do{
  17. b=(byte)finp.read();
  18. System.out.print((char)b);
  19. }
  20. while(b!=-1);
  21. finp.close();
  22. }
  23. }
  24. }

The file, if anyone interested is something like
  1. String 1
  2. Line 2
  3. Blablabla
  4. The sound of the sea
Last edited by Lioshenka; Feb 22nd, 2008 at 2:09 pm.
Reply With Quote Quick reply to this message  
Join Date: May 2007
Posts: 4,438
Reputation: Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of 
Solved Threads: 510
Moderator
Featured Poster
Ezzaral's Avatar
Ezzaral Ezzaral is offline Offline
Industrious Poster

Re: How to output random line from a file

 
0
  #2
Feb 22nd, 2008
Use a BufferedReader with the readLine() method. You can either skip lines as you read them, or place each line into an ArrayList, and process them as needed.
Reply With Quote Quick reply to this message  
Join Date: May 2007
Posts: 20
Reputation: Lioshenka is an unknown quantity at this point 
Solved Threads: 0
Lioshenka Lioshenka is offline Offline
Newbie Poster

Re: How to output random line from a file

 
0
  #3
Feb 22nd, 2008
How do I do it??? I've just tried to do something like that, and this is what I came up with

  1. package Talking;
  2.  
  3. import java.io.BufferedReader;
  4. import java.io.IOException;
  5. import java.io.FileNotFoundException;
  6. import java.io.FileReader;
  7.  
  8.  
  9.  
  10. public class Talking{
  11.  
  12. public static void main(String[] args) throws FileNotFoundException{
  13. int start;
  14. int end;
  15. start=0;
  16. end=10;
  17. int num = (int)(start + (Math.random() * (end-start)));
  18. System.out.println(num);
  19.  
  20.  
  21.  
  22. public void readLineFromFile() {
  23.  
  24. try {
  25. BufferedReader br = new BufferedReader(new FileReader("talking.txt"));
  26. String thisLine = br.readLine();
  27. System.out.println(thisLine);
  28. } // end try
  29.  
  30. catch (IOException e) {
  31. System.err.println("Error: " + e);
  32. }
  33. }
  34.  
  35. }
Now, how can I pass the num integer as an arguement for the readLine method? Am I thinking in a right direction?

I print out the random value just to be sure it works. I will comment it out later.
Last edited by Lioshenka; Feb 22nd, 2008 at 3:27 pm.
Reply With Quote Quick reply to this message  
Join Date: May 2007
Posts: 4,438
Reputation: Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of 
Solved Threads: 510
Moderator
Featured Poster
Ezzaral's Avatar
Ezzaral Ezzaral is offline Offline
Industrious Poster

Re: How to output random line from a file

 
0
  #4
Feb 22nd, 2008
readLine() just sequentially reads lines from the file. If you only care about certain lines, maintain a counter as the lines are read and print/store/whatever lines you need based upon that counter.
  1. int lineNumber=0;
  2. String line=null;
  3. while ( (line = br.readLine()) != null){
  4. lineNumber++;
  5. if ( <some expression on lineNumber> ){
  6. // do something
  7. }
  8. }
Reply With Quote Quick reply to this message  
Join Date: May 2007
Posts: 4,438
Reputation: Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of 
Solved Threads: 510
Moderator
Featured Poster
Ezzaral's Avatar
Ezzaral Ezzaral is offline Offline
Industrious Poster

Re: How to output random line from a file

 
0
  #5
Feb 22nd, 2008
If you want to randomly present lines from the file, I would read all of the lines into an ArrayList first and then get() them with a random int of the range 0 to size()-1.
Reply With Quote Quick reply to this message  
Join Date: May 2007
Posts: 20
Reputation: Lioshenka is an unknown quantity at this point 
Solved Threads: 0
Lioshenka Lioshenka is offline Offline
Newbie Poster

Re: How to output random line from a file

 
0
  #6
Feb 22nd, 2008
Thanks a lot, I got it to work now!
Reply With Quote Quick reply to this message  
Join Date: Jun 2006
Posts: 7,609
Reputation: ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of 
Solved Threads: 464
Super Moderator
Featured Poster
~s.o.s~'s Avatar
~s.o.s~ ~s.o.s~ is offline Offline
Failure as a human

Re: How to output random line from a file

 
0
  #7
Feb 23rd, 2008
Look into the RandomAccessFile class. The only downside being the file is a treated as a sequence of bytes rather than the conventional line oriented view which many are used to. This class is especially for example useful when you want to come up with your custom file format which would have a fixed header size.
I don't accept change; I don't deserve to live.
Reply With Quote Quick reply to this message  
Join Date: May 2007
Posts: 20
Reputation: Lioshenka is an unknown quantity at this point 
Solved Threads: 0
Lioshenka Lioshenka is offline Offline
Newbie Poster

Re: How to output random line from a file

 
0
  #8
Feb 23rd, 2008
Simple question now Now I have made another program, which reads 85 random characters from file one by one and outputs them onto the screen. The thing is, that for some reason the output is produced on a number of lines, instead of just one. I dont know what causes it. The number of lines can vary, e.g. with 85 characters output it could be 2 lines, up to about 7. Any suggestions, please?

If there isnt a very simple solution, just say so, this is not crucial - my tutor only asks for random characters output
Last edited by Lioshenka; Feb 23rd, 2008 at 8:05 pm.
Reply With Quote Quick reply to this message  
Join Date: Jun 2006
Posts: 7,609
Reputation: ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of 
Solved Threads: 464
Super Moderator
Featured Poster
~s.o.s~'s Avatar
~s.o.s~ ~s.o.s~ is offline Offline
Failure as a human

Re: How to output random line from a file

 
0
  #9
Feb 24th, 2008
I am not quite sure that I understand what you are trying to say here but if you are worried about the number of characters displayed per line on your console window, it depends on the properties of the console window (OS specific) and has got nothing to do with your program. For e.g. in Windows you can set the properties of the console window like Screen buffer and window size to increase / display the number of characters displayed.

If still in doubt, post the code along with the varying output you are talking about.
I don't accept change; I don't deserve to live.
Reply With Quote Quick reply to this message  
Join Date: May 2007
Posts: 20
Reputation: Lioshenka is an unknown quantity at this point 
Solved Threads: 0
Lioshenka Lioshenka is offline Offline
Newbie Poster

Re: How to output random line from a file

 
0
  #10
Feb 24th, 2008
I think, I got it while I was lying in the bed yesterday. Does /n (line break) counts as a character??? If yes, is there an easy way to avoid reading it?

PS the lines in console window break wherever they want, so for example the sample output would be like
asd as iasd. ad
as
hfb jd
o8 fg.dnc;
Last edited by Lioshenka; Feb 24th, 2008 at 11:08 am.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC