Random Line reading in text file

I am trying to read a random line in a text file but every time i read it it reads first line of the file any one can help me what is the problem in the code is or provide me with a new code.

BufferedReader in = new BufferedReader(new InputStreamReader(System.in));

File file = new File("word.txt");

if(!file.exists())

{

System.out.println("File does not exist.");

System.exit(0);

}

try{

//Open the file for reading

RandomAccessFile raf = new RandomAccessFile(file, "r");

raf.seek(file.length());//Seek to end of file

System.out.println("Read full line: " + raf.readLine());

//str2 is a String

str2 = str2 + raf.readLine();

System.out.println(""+str2);

raf.close();

System.out.println("Successfully");

}

catch(IOException ioe)

{

System.out.println(ioe.getMessage());

}

my text file look like in this format

mind

hate

Abhor

Bigot

narrow

prejudiced

person

Counterfeit

fake

false

give

voting

rights

Hamper

hinder

obstruct

Kindle

to

start

fire

harmful

poisonous

lethal

[1258 byte] By [javedkarima] at [2007-11-27 2:44:28]
# 1
Hi, RandomAccessFille is not used to read one random line in yourfile. for that, you have to count lines in your file and pick a random number between 1 and the number of line (see Random)to select a line.Hope that help,Jack
jack@square6a at 2007-7-12 3:11:06 > top of Java-index,Java Essentials,Java Programming...
# 2

Next time when posting code, please use code tags: http://forum.java.sun.com/help.jspa?sec=formatting

A RandomAccessFile has nothing to do with getting a (pseudo) random line number from a file.

You should also split things up in separate methods:

- a method for counting the number of lines in a file;

- a method to get the N-th line from a file.

In your main method, you call the method which counts the number of lines N in a file and then generate a (pseudo) random number between 1 and N. Use the java.util.Random class for this.

When you have generated that number, call the other method to get that specific line number.

Here's a small start:

import java.io.*;

import java.util.*;

public class Main {

public static String getLineNumber(int number) throws FileNotFoundException {

// your code here

}

public static int countLines(String fileName) throws FileNotFoundException {

// your code here

}

public static void main(String[] args) throws Exception {

int numberLines = countLines("word.txt");

Random generator = new Random();

int randomNumber = generator.nextInt(numberLines)+1;

String randomLine = getLineNumber(randomNumber);

System.out.println("Line "+randomNumber+" = '"+randomLine+"'");

}

}

In both methods, you can use the java.util.Scanner class to read lines from the file.

Have a look at the API docs of the Scanner class:

http://java.sun.com/j2se/1.5.0/docs/api/java/util/Scanner.html (including code samples!)

Good luck.

prometheuzza at 2007-7-12 3:11:06 > top of Java-index,Java Essentials,Java Programming...
# 3

all i have to do is to read a random line from the text line and store it in array

can you please provide the code for

public static String getLineNumber(int number) throws FileNotFoundException {

// your code here

}

public static int countLines(String fileName) throws FileNotFoundException {

// your code here

}

javedkarima at 2007-7-12 3:11:06 > top of Java-index,Java Essentials,Java Programming...
# 4
You're welcome for the previous answer!> all i have to do is to read a random line from the> text line and store it in array> can you please provide the code for>...It's your homework, not mine. You do it yourself.
prometheuzza at 2007-7-12 3:11:06 > top of Java-index,Java Essentials,Java Programming...
# 5
i am little busy in my work i also have to do other projects. it is the part of the project which in not working well. If you kindly help me in making the code i shall be very thanks full. I am making Hangman game only this part was troubling me a lot. i have done every thing except this.
javedkarima at 2007-7-12 3:11:06 > top of Java-index,Java Essentials,Java Programming...
# 6

> i am little busy in my work i also have to do other

> projects. it is the part of the project which in not

> working well. If you kindly help me in making the

> code i shall be very thanks full.

Sorry but your being 'busy' is not our problem. Since this is obviously homework it would not be ethical for us to give you the code. You have already be told that using RandomAccessFile is not the way to go yet you keep expecting to use it.

Read the lines of the file when your program starts and store the lines in an ArrayList. It is then very easy to get a random line using java.util.Random.

sabre150a at 2007-7-12 3:11:06 > top of Java-index,Java Essentials,Java Programming...
# 7

>i am little busy in my work i also have to do other projects. it is the part of the

>project which in not working well. If you kindly help me in making the code i

>shall be very thanks full. I am making Hangman game only this part was

>troubling me a lot. i have done every thing except this.

Ofcourse nobdy else has anything to do but to serve slackers..

Anyhow the code is really easy to fix. You only need to learn what the metod readLine does.

I.e. readLine(); reads the first line of an file, it also moves the row counter up by 1. That means the next time you call for readLine(); you will read the next line in the textfile.

Examplecode

BufferedReader br = new BufferedReader(new FileReader("File with path.txt"));

String txtLine = "";

txtLine = br.readLine(); //Gives first line in the txt file

txtLine = br.readLine(); //Gives second line in the txt file

txtLine = br.readLine(); //Gives third line in the txt file

and so on.

Here is even the random maker:

int random = (int) ((int) 1000 * Math.random()); //will give an number between 0 and 1000. or is it from 0 - 1000? hmmm... aint that important atm..

That is all you need to understand... Now "just" make it read an random row.

I will give you an hint. Use an for loop.

prigasa at 2007-7-12 3:11:06 > top of Java-index,Java Essentials,Java Programming...
# 8
is RandomAccessFile class slower than the normal FileInputStream or FileOutputStream when using read(byte[]) or write(byte[]) methods? or they have the same speed?
el3oriana at 2007-7-12 3:11:06 > top of Java-index,Java Essentials,Java Programming...