I am trying to read a random line from a file and if that line is null it will create a random string.
It works with files with upto and including 5 lines. To test it I made a 2 line file:
1-hello
2-but
However, when run it the output is this - Italics is user imput, file name, the number is the file line to read:
test
0
but
but
test
test
4
but
but
test
test
1
but
but
It also needs the suer to enter test twice, not once.
here is the code:
if(f.isFile()){
exists = true;
//creates a random number - this is the line number to read
int check_phrase = new Random().nextInt(5);
System.out.println(check_phrase);
if (check_phrase < 1){
check_phrase = 1;
}
//reads file lines until chosen line is found
try {
BufferedReader r = new BufferedReader(new FileReader(f));
int c = 1;
while(r.readLine() != null && c<= check_phrase ){
response = r.readLine();
System.out.println(response);
c++;
}
//checks if response is null
if(response.length() < 2){
generateword();
}else{
usedfile = true;
}
}