| | |
How to output random line from a file
![]() |
•
•
Join Date: May 2007
Posts: 20
Reputation:
Solved Threads: 0
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:
The file, if anyone interested is something like
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:
Java Syntax (Toggle Plain Text)
package Talking; import java.io.*; public class Talking{ public static void main(String[] args) throws IOException{ File f; f=new File("Talking.txt"); if(!f.exists()&& f.length()<0) System.out.println("The specified file does not exist"); else{ FileInputStream finp=new FileInputStream(f); byte b; do{ b=(byte)finp.read(); System.out.print((char)b); } while(b!=-1); finp.close(); } } }
The file, if anyone interested is something like
Java Syntax (Toggle Plain Text)
String 1 Line 2 Blablabla The sound of the sea
Last edited by Lioshenka; Feb 22nd, 2008 at 2:09 pm.
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.
•
•
Join Date: May 2007
Posts: 20
Reputation:
Solved Threads: 0
How do I do it??? I've just tried to do something like that, and this is what I came up with
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.
Java Syntax (Toggle Plain Text)
package Talking; import java.io.BufferedReader; import java.io.IOException; import java.io.FileNotFoundException; import java.io.FileReader; public class Talking{ public static void main(String[] args) throws FileNotFoundException{ int start; int end; start=0; end=10; int num = (int)(start + (Math.random() * (end-start))); System.out.println(num); public void readLineFromFile() { try { BufferedReader br = new BufferedReader(new FileReader("talking.txt")); String thisLine = br.readLine(); System.out.println(thisLine); } // end try catch (IOException e) { System.err.println("Error: " + e); } } }
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.
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.
Java Syntax (Toggle Plain Text)
int lineNumber=0; String line=null; while ( (line = br.readLine()) != null){ lineNumber++; if ( <some expression on lineNumber> ){ // do something } }
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.
•
•
Join Date: May 2007
Posts: 20
Reputation:
Solved Threads: 0
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
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.
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.
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.
•
•
Join Date: May 2007
Posts: 20
Reputation:
Solved Threads: 0
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;
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.
![]() |
Similar Threads
- File I/O (Reading from a Random-Access File) NEW (C++)
- Random Salary (Java)
- Input/Output Code Help Please! (C++)
- splitting files: seemingly random error disappears in debug mode (Python)
- Reading in a random line from a file (C++)
- C++ How can read from file.txt & where can save this file(file.txt) to start reading (C++)
- File operations (C)
- parse a .txt file (Java)
- Help, record arrays (VB.NET)
Other Threads in the Java Forum
- Previous Thread: New Line......?
- Next Thread: basic questions
| Thread Tools | Search this Thread |
911 actionlistener addressbook android api append applet application array arrays automation binary blackberry block bluetooth character chat class client code component consumer csv database desktop developmenthelp eclipse error fractal ftp game givemetehcodez graphics gui html ide image integer j2me j2seprojects japplet java javaarraylist javac javaee javaprojects jni jpanel julia lego linked linux list loops mac map method methods mobile netbeans newbie number objects online oriented panel printf problem program programming project projects properties recursion replaydirector reporting researchinmotion rotatetext rsa scanner se server set singleton sms sort sql string swing test textfields threads time title tree tutorial-sample ubuntu update windows working






