3 Replies - 6099 Views - Last Post: 16 December 2009 - 05:23 PM Rate Topic: -----

#1 JustSomeBeginner  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 14
  • Joined: 20-October 09

Read Random Text File Line

Posted 15 December 2009 - 07:26 AM

this is for a school project: it will read students names in and ask if you want to see a random name.

this is what I have so far, to store what it reads into a array, now I need a way to pick 1 random line and output it. There needs to be an option but I have no idea how to make one, and have tried .getRandom(); for a name but have no idea for that either.

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.FileNotFoundException;
import java.io.IOException;

public class TextFile
{
	public static void main(String[] args)
	{
	
		File file = new File("/Users/Period_3/Desktop/test.txt");
		StringBuffer contents = new StringBuffer();
		BufferedReader reader = null;

		try
		{
			reader = new BufferedReader(new FileReader(file));
			String text = null;
			
			String strLine;
			String[] myarray;
			myarray = new String[10];

			// all lines are read
			while ((text = reader.readLine()) != null)
			{
				contents.append(text)
					.append(System.getProperty(
						"line.separator"));
			}
		} catch (FileNotFoundException e)
		{
			e.printStackTrace();
		} catch (IOException e)
		{
			e.printStackTrace();
		} finally
		{
			try
			{
				if (reader != null)
				{
					reader.close();
				}
			} catch (IOException e)
			{
				e.printStackTrace();
			}
		}
		
		// show file contents here
		System.out.println(contents.toString());
		
		
	}
}



Is This A Good Question/Topic? 0
  • +

Replies To: Read Random Text File Line

#2 painkiller102  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 15
  • View blog
  • Posts: 281
  • Joined: 27-February 08

Re: Read Random Text File Line

Posted 15 December 2009 - 07:43 AM

View PostJustSomeBeginner, on 15 Dec, 2009 - 06:26 AM, said:

this is for a school project: it will read students names in and ask if you want to see a random name.

this is what I have so far, to store what it reads into a array, now I need a way to pick 1 random line and output it. There needs to be an option but I have no idea how to make one, and have tried .getRandom(); for a name but have no idea for that either.

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.FileNotFoundException;
import java.io.IOException;

public class TextFile
{
	public static void main(String[] args)
	{
	
		File file = new File("/Users/Period_3/Desktop/test.txt");
		StringBuffer contents = new StringBuffer();
		BufferedReader reader = null;

		try
		{
			reader = new BufferedReader(new FileReader(file));
			String text = null;
			
			String strLine;
			String[] myarray;
			myarray = new String[10];

			// all lines are read
			while ((text = reader.readLine()) != null)
			{
				contents.append(text)
					.append(System.getProperty(
						"line.separator"));
			}
		} catch (FileNotFoundException e)
		{
			e.printStackTrace();
		} catch (IOException e)
		{
			e.printStackTrace();
		} finally
		{
			try
			{
				if (reader != null)
				{
					reader.close();
				}
			} catch (IOException e)
			{
				e.printStackTrace();
			}
		}
		
		// show file contents here
		System.out.println(contents.toString());
		
		
	}
}



First off, you are not using your array!!! Its just called but never assigned anything! What you need to do first is get that array up and running by:

String strLine;
String[] myarray;
myarray = new String[10];
int location = 0;
while((strLine = reader.readLine())!= null)
{
		 myarray[locatoin] = strLine;
		 location++;
}



You must be careful that your array has enough elements to store all the information you want!! Otherwise you will get an array out of index error! Now as for the random part, lets make a new class shall we?

public int getRandomNameLocation(int arrayLength)
{
	  Random ran = new Random();
	  int location = ran.nextInt(arrayLenght);
	  return location;
}



Now all you do is call that after your while loop. SO:

String strLine;
String[] myarray;
myarray = new String[10];
int location = 0;
while((strLine = reader.readLine())!= null)
{
		 myarray[locatoin] = strLine;
		 location++;
}
System.out.println(myarray[getRandomNameLocation(location)]);



And that should print out a nice and random name from the array! It may need a little tweaking tho ^_~ just did this off the top of my head, but this is the idea of how to achieve your goal!
Was This Post Helpful? 0
  • +
  • -

#3 JustSomeBeginner  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 14
  • Joined: 20-October 09

Re: Read Random Text File Line

Posted 16 December 2009 - 07:24 AM

View Postpainkiller102, on 15 Dec, 2009 - 06:43 AM, said:

View PostJustSomeBeginner, on 15 Dec, 2009 - 06:26 AM, said:

this is for a school project: it will read students names in and ask if you want to see a random name.

this is what I have so far, to store what it reads into a array, now I need a way to pick 1 random line and output it. There needs to be an option but I have no idea how to make one, and have tried .getRandom(); for a name but have no idea for that either.

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.FileNotFoundException;
import java.io.IOException;

public class TextFile
{
	public static void main(String[] args)
	{
	
		File file = new File("/Users/Period_3/Desktop/test.txt");
		StringBuffer contents = new StringBuffer();
		BufferedReader reader = null;

		try
		{
			reader = new BufferedReader(new FileReader(file));
			String text = null;
			
			String strLine;
			String[] myarray;
			myarray = new String[10];

			// all lines are read
			while ((text = reader.readLine()) != null)
			{
				contents.append(text)
					.append(System.getProperty(
						"line.separator"));
			}
		} catch (FileNotFoundException e)
		{
			e.printStackTrace();
		} catch (IOException e)
		{
			e.printStackTrace();
		} finally
		{
			try
			{
				if (reader != null)
				{
					reader.close();
				}
			} catch (IOException e)
			{
				e.printStackTrace();
			}
		}
		
		// show file contents here
		System.out.println(contents.toString());
		
		
	}
}



First off, you are not using your array!!! Its just called but never assigned anything! What you need to do first is get that array up and running by:

String strLine;
String[] myarray;
myarray = new String[10];
int location = 0;
while((strLine = reader.readLine())!= null)
{
		 myarray[locatoin] = strLine;
		 location++;
}



You must be careful that your array has enough elements to store all the information you want!! Otherwise you will get an array out of index error! Now as for the random part, lets make a new class shall we?

public int getRandomNameLocation(int arrayLength)
{
	  Random ran = new Random();
	  int location = ran.nextInt(arrayLenght);
	  return location;
}



Now all you do is call that after your while loop. SO:

String strLine;
String[] myarray;
myarray = new String[10];
int location = 0;
while((strLine = reader.readLine())!= null)
{
		 myarray[locatoin] = strLine;
		 location++;
}
System.out.println(myarray[getRandomNameLocation(location)]);



And that should print out a nice and random name from the array! It may need a little tweaking tho ^_~ just did this off the top of my head, but this is the idea of how to achieve your goal!





so it should look like this? Im getting an error at the ARRAY LENGTH

/**
 * Stephen Lambert
 * 
 *  Working to read text file.
 *  Needs to pick a random name.
 */

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Random;

public class TextFile
{
	public static void main(String[] args)
	{
	
		File file = new File("/Users/Period_3/Desktop/test.txt");
		StringBuffer contents = new StringBuffer();
		BufferedReader reader = null;

		try
		{
			reader = new BufferedReader(new FileReader(file));
			String text = null;
			
			
			//ARRAY
			String strLine;
			String[] myarray;
			myarray = new String[50];
			int location = 0;
			while((strLine = reader.readLine())!= null)
			{
					 myarray[location] = strLine;
					 location++;
			}
			
			
			// ARRAY LENGTH (PR?)
			int getRandomNameLocation(int arrayLength)
			{
				  Random ran = new Random();
				  location = ran.nextInt(arrayLength);
				  return;
			}
			
			
			
			
			// all lines are read
			while ((text = reader.readLine()) != null)
			{
				contents.append(text)
					.append(System.getProperty(
						"line.separator"));
			}
		} catch (FileNotFoundException e)
		{
			e.printStackTrace();
		} catch (IOException e)
		{
			e.printStackTrace();
		} finally
		{
			try
			{
				if (reader != null)
				{
					reader.close();
				}
			} catch (IOException e)
			{
				e.printStackTrace();
			}
		}
		
		// show file contents here
		System.out.println(contents.toString());
		
		
		
		
	}
}

Was This Post Helpful? 0
  • +
  • -

#4 erik.price  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 485
  • View blog
  • Posts: 2,690
  • Joined: 18-December 08

Re: Read Random Text File Line

Posted 16 December 2009 - 05:23 PM

int getRandomNameLocation(int arrayLength)
			{
				  Random ran = new Random();
				  location = ran.nextInt(arrayLength);
				  return;
			}

Are you trying to define a new method here?

If so, you need to specify a return type (void, double, char, String, etc.)
And place it outside of the main method.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1