School Assignment? Project Due Tomorrow? Chat LIVE With A Programming Expert!
Welcome to Dream.In.Code
Become an Expert!

Join 340,162 Programmers for FREE! Get instant access to thousands of experts, tutorials, code snippets, and more! There are 4,036 people online right now. Registration is fast and FREE... Join Now!



print a random line from a text file

print a random line from a text file Rate Topic: -----

#1 javarookie0  Icon User is offline

  • New D.I.C Head
  • Pip
  • Group: Members
  • Posts: 18
  • Joined: 23-January 09


Dream Kudos: 0

Posted 09 April 2009 - 09: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");
			
		}
	   
	}
	
}



Was This Post Helpful? 0
  • +
  • -


#2 markhazlett9  Icon User is offline

  • Coding is a lifestyle
  • Icon
  • View blog
  • Group: Greeters
  • Posts: 1,659
  • Joined: 12-July 08


Dream Kudos: 25

Posted 09 April 2009 - 09: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
  • Pip
  • Group: Members
  • Posts: 18
  • Joined: 23-January 09


Dream Kudos: 0

Posted 09 April 2009 - 10: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 - 10:09 AM

Was This Post Helpful? 0
  • +
  • -

#4 markhazlett9  Icon User is offline

  • Coding is a lifestyle
  • Icon
  • View blog
  • Group: Greeters
  • Posts: 1,659
  • Joined: 12-July 08


Dream Kudos: 25

Posted 09 April 2009 - 10: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
  • Icon
  • Group: Expert w/DIC++
  • Posts: 1,187
  • Joined: 06-March 09


Dream Kudos: 150

Posted 09 April 2009 - 06: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
  • Pip
  • Group: Members
  • Posts: 18
  • Joined: 23-January 09


Dream Kudos: 0

Posted 10 April 2009 - 05: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
  • Icon
  • Group: Expert w/DIC++
  • Posts: 1,187
  • Joined: 06-March 09


Dream Kudos: 150

Posted 10 April 2009 - 07: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
  • +
  • -



Fast Reply

  

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users



Live Help!

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter Fan Us On Facebook

Tutorials

Programming

Web Development

Reference Sheets

Code Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month