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;
                            }
                            }

you're using methods in there of which we don't know the code. do you expect us to guess?

It works with files with upto and including 5 lines.

what do you mean by that? yes, it does, since you coded it that way.

you're saying that the user needs to enter 'test' twice, but nothing in the snippet above has user input in it.

The user enters the name of the file befor the if statement
generateresponse(); generates a random string

that'ssssss briliant. but saying something is implemented like 'x' doesn't mean it can't be implemented like 'y'. obviously there's something in your code that's not going the way you planned it, and as far as I can see, a lot of that is going on in the code you're not showing.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.