I'm trying to make a method that a random line from lol.txt (which has 113 lines) would be chosen and sent out as message box. How it should work:
- Generates random number from 0 till 112
- a for loop should go over the random number of lines
- output the randomly generated line as a message box
In my case step 2 doesn't work so I was hoping that someone could suggest on that. Here's the code:
public void close(){
try{
Random random = new Random();
int randomInt = random.nextInt(112);
FileReader fr = new FileReader("lol.txt");
BufferedReader reader = new BufferedReader(fr);
String line = reader.readLine();
Scanner scan = null;
for (int i = 0; i < randomInt + 1; i++) {
scan = new Scanner(line);
line = scan.nextLine();
}
JOptionPane.showMessageDialog(null,line);
}catch (IOException e){
JOptionPane.showMessageDialog(null,e.getMessage()+" for lol.txt","File Error",JOptionPane.ERROR_MESSAGE);
}
}
If you want to send me the solution with an array list that's fine but I would really like it to be how I planned it initially.