0

Very new to Java (only a few days into learning) I'm looking to make a random quotes program. I have the quotes on separate lines in the file quotes.txt. What I need to be able to do is grab a random line and print it.

I figure the steps are to first determine the number of lines in the file, then generate a random number between 0 and the number of lines. Then go to that line in the file and print it.

I just have no idea how to even get started really (again, forgive me, very new to Java). Any help is much appreciated.

quibblify
  • 395
  • 2
  • 6
  • 13
  • 3
    [See this](http://stackoverflow.com/questions/2218005/how-to-get-a-random-line-of-a-text-file-in-java) – SerCrAsH May 26 '15 at 17:54
  • Using a database might make this easier – ControlAltDel May 26 '15 at 17:55
  • 2
    @ControlAltDel Please explain. No idea how that could possibly make this easier. Sounds needlessly complex. – tnw May 26 '15 at 17:56
  • i suggest you to learn the basics good and control them before jumping to it. you might miss a lot of knowledge and just do copy-paste a code without understanding. –  May 26 '15 at 17:58
  • If you load all of your lines into the database using an auto assigned index, you won't have to split your string every time in Java. You can make two sql calls - one to get the max value of the table, and the other with your random value – ControlAltDel May 26 '15 at 18:04

3 Answers3

2

What I would do is create an ArrayList and add the lines in there. Get a random number between 0 and (the size of the ArrayList - 1), and get the value of the information stored at that index. I will leave the code for you to try and figure out, however I will help when you post your code you have already written.

jbokwx
  • 195
  • 1
  • 2
  • 14
1

Here is a quick idea. Note I have not tested this code. Just put it together really quick... And this should be sufficient only for small files. If you need to handle larger amounts of data, then I would suggest just reading the file to the line of interest (based on random) and processing only that line. Moreover, other libs may help specifically with this problem (e.g., Apache commons: FileUtils.readLines(file).get(indexNumber))

FileInputStream fs= new FileInputStream("quotes.txt");
BufferedReader br = new BufferedReader(new InputStreamReader(fs));
ArrayList<String> array = new ArrayList<>();
String line;
while((line = br.readLine()) != null)
  array.add(line);
// variable so that it is not re-seeded every call.
Random rand = new Random();

// nextInt is exclusive. Should be good with output for array.
int randomIndex = rand.nextInt(array.size());

// Print your random quote... 
System.out.println(array.get(randomIndex));
Nibras Reeza
  • 127
  • 1
  • 6
Tr1gZer0
  • 1,482
  • 11
  • 18
0
  1. Read the file into a List or array. A Scanner or BufferedReader can accomplish this.
  2. Use the Random class to generate a random number between 0 (inclusive) and the size of the array/List (exclusive).
  3. Use the result from (2) to access the index of that element in the array/List from (1).
copeg
  • 8,290
  • 19
  • 28