6 Replies - 8790 Views - Last Post: 10 April 2009 - 08:09 PM Rate Topic: -----

#1 javarookie0  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 18
  • Joined: 23-January 09

print a random line from a text file

Posted 09 April 2009 - 10:15 AM

Hi,

I do not understand how to print a random line from a text file. So far in my program i have been able to read the file but not to print out a random line


import java.util.Random;
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;

public class filereading {

	public static void main (String args []) {
	 
		File file = new File("MyPhrases.txt");
		
		int count=0;
		try {
			
			Scanner scanner = new Scanner(file);
			while (scanner.hasNextLine()) {
				
				String line = scanner.nextLine();
		   //   int intLine = Integer.parseInt(line);			  
		   //   Random random = new Random();
		   //   String word = random.nextLine(line); 
		   //   System.out.println(line);
				count++;
		   //   System.out.println(count);
		   
			Random random = new Random();
		int rint = random.nextInt(count);
		System.out.println(count);
		
		}
			
		} catch (FileNotFoundException e) {
			
			System.out.println("error");
			
		}
	   
	}
	
}




Is This A Good Question/Topic? 0
  • +

Replies To: print a random line from a text file

#2 markhazlett9  Icon User is offline

  • Coding is a lifestyle
  • member icon

Reputation: 60
  • View blog
  • Posts: 1,666
  • Joined: 12-July 08

Re: print a random line from a text file

Posted 09 April 2009 - 10:46 AM

Text files are meant to be read in sequential order. Unless you use a random access file there is not way to just read a random line. If you want to say read line 53 from a text file you can do this....

int counter =0;

while(file.hasNext())
{
if(counter == 53)
{
return something;
}
counter++;
}


Sorry for the formatting, don't have an IDE available. But that should do what you need. Cheers
Was This Post Helpful? 0
  • +
  • -

#3 javarookie0  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 18
  • Joined: 23-January 09

Re: print a random line from a text file

Posted 09 April 2009 - 11:08 AM

so how will i be able to print a random line when using Random access file? Could you show me this in code please?

This post has been edited by javarookie0: 09 April 2009 - 11:09 AM

Was This Post Helpful? 0
  • +
  • -

#4 markhazlett9  Icon User is offline

  • Coding is a lifestyle
  • member icon

Reputation: 60
  • View blog
  • Posts: 1,666
  • Joined: 12-July 08

Re: print a random line from a text file

Posted 09 April 2009 - 11:17 AM

That's a pretty complicated piece of code. What a random access file does is allow you to read items from a specific byte of code. So if you say go to byte 4* RECORD_SIZE it will proceed to that location and then you go per say, readInteger and it will read an integer. Here a link to the java tutorial on random access files. Try it out and if you're having any issues post your code on here and we'll help you out... Here's the link


http://java.sun.com/...al/io/rafs.html
Was This Post Helpful? 0
  • +
  • -

#5 Fuzzyness  Icon User is offline

  • Comp Sci Student
  • member icon

Reputation: 669
  • View blog
  • Posts: 2,438
  • Joined: 06-March 09

Re: print a random line from a text file

Posted 09 April 2009 - 07:46 PM

No i wouldnt do it that way.Open the file, and scan the line into a string. Make an arraylist of strings, and add the string to it. once its done. make a random that generates 0- max elements in the arraylist. then simply print what is at that element.Hope this helps!
Was This Post Helpful? 0
  • +
  • -

#6 javarookie0  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 18
  • Joined: 23-January 09

Re: print a random line from a text file

Posted 10 April 2009 - 06:39 AM

Thats the way i wanted to do it, but i couldn't seem to get the hang of doing it. If you could show me this in like code in an example that would be really helpful.

Thanks for your help.
Was This Post Helpful? 0
  • +
  • -

#7 Fuzzyness  Icon User is offline

  • Comp Sci Student
  • member icon

Reputation: 669
  • View blog
  • Posts: 2,438
  • Joined: 06-March 09

Re: print a random line from a text file

Posted 10 April 2009 - 08:09 PM

Hmmmmm..... I suppose :)
ArrayList<String> sentences = new ArrayList<String>();
String temp;
int numLines;
JFileChooser chooser = new JFileChooser();
   		File infile = null;
   		FileReader reader = null;
   		if (chooser.showOpenDialog(null) == JFileChooser.APPROVE_OPTION)
   		{
   			infile = chooser.getSelectedFile();
   			reader = new FileReader(infile);
   		}
		Scanner input = new Scanner(infile);
		while (input.hasNextLine())
{temp = input.nextLine();
sentences.add(temp);
numLines++;} 
Random r = new Random();
System.out.println(sentences.get(r.nextInt(sentences.size())));


Breakdown :

1. JFileChooser allows the user to select a file to read the sentences from

2. It assign the File the selected file and the reader is assign a file to read!

3. Scanner is then created using the FIle selected, and using the hasNextLine() method, we make a while loop to keep cycling through every line adding it into the arraylist, but increase numLines as we go through it.

4. We then create a random object. (You will have to import Random) ArrayList has a method get() that allows us to retrieve something at that element. In this case we are going to get the string, and print it. Hence why it is in a System.out.println call

Hope this helps!
Goodluck!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1