patternjavaMinor
Picking random lines out of text files
Viewed 0 times
randompickingtextfilesoutlines
Problem
So I was doing something which creates an airplane with an actual airline code, number, airplane type and so on.
The airline code and airplane type are stored in a txt file so I can add more later to my leisure... but the code I came up with to get a random line is extremely messy. Is there a better way? I searched all over stackexchange but it all seemed quite complex for little files which will have only (if ever) a couple dozen lines of content.
The airline code and airplane type are stored in a txt file so I can add more later to my leisure... but the code I came up with to get a random line is extremely messy. Is there a better way? I searched all over stackexchange but it all seemed quite complex for little files which will have only (if ever) a couple dozen lines of content.
public String generate() {
int totalLines = 0;
File file = new File("icaocodes.txt");
BufferedReader br = null;
try {
br = new BufferedReader(new FileReader(file));
while ((br.readLine()) != null) {
totalLines++;
}
br.close();
br = new BufferedReader(new FileReader(file));
Random random = new Random();
int randomInt = random.nextInt(totalLines);
int count=0;
String icaocode;
while ( (icaocode = br.readLine()) != null) {
if (count == randomInt) {
br.close();
return icaocode;
}
count++;
}
br.close();
} catch (FileNotFoundException e) {
System.out.println("File not found: " + file.toString());
} catch (IOException e) {
System.out.println("Unable to read file: " + file.toString());
}
return "Puppies";
}Solution
Naive Solution
(I say naive because of the assumption that the file you are working with is small. From a general design standpoint, it is usually best to prepare for the worst case and assume you may have to read a super long file at some point in time.)
Read the file line by line into a List or a Map, then fetch a random entry from here. (Judging by the brief details of what you're working on, a Map of different objects might be a good choice if you'd like to parse a file into different objects for flights, etc.)
Probably Better Solution
There's a good discussion over here about Reservoir Sampling.
Side notes
Because you posted in Code Review...
(I say naive because of the assumption that the file you are working with is small. From a general design standpoint, it is usually best to prepare for the worst case and assume you may have to read a super long file at some point in time.)
Read the file line by line into a List or a Map, then fetch a random entry from here. (Judging by the brief details of what you're working on, a Map of different objects might be a good choice if you'd like to parse a file into different objects for flights, etc.)
Probably Better Solution
There's a good discussion over here about Reservoir Sampling.
Side notes
Because you posted in Code Review...
- Split the reading from the BufferedReaders into separate try-catch blocks. This is just good practice, as in some cases your program may be able to continue executing even if an exception is thrown.
- Close your readers in a finally. Check out this post on closing readers
- Make
filefinal.
- I'm assuming you just slapped the "Puppies" in the return statement, but if you post more complete code, you can get comments and suggestions on all of your code, not just your approach to finding a random line :)
Context
StackExchange Code Review Q#85429, answer score: 7
Revisions (0)
No revisions yet.