0

I've been trying to code a quiz game in javafx where I store the questions on a text file and then randomize a number then use it to call the line of the same number on the text file and read it into an array.

After looking online I can only seem to find how to read a text file line by line instead of a specific line. I also use the following code to read the text file but am unsure where to go on from there.

File file = new File("/Users/administrator/Desktop/Short Questions.txt");
FileReader fileReader = new FileReader(file);
BufferedReader bufferedReader = new BufferedReader(fileReader);
String line;
3
  • 1
    Please refer docs.oracle.com/javase/8/docs/api/java/nio/file/… You can read the lines and store them in a List. You can then take a random element from the list. Commented Dec 26, 2019 at 7:28
  • There is no way of just pulling a single line from a text file without reading all of the lines, unless additional information is available (byte offset of each line). Without that kind of info line n could start at the (n-1)th char or at an arbitrary offset that is larger (restricted by the total file size of course): Lines could be empty or contain an arbitrary number of characters. Commented Dec 26, 2019 at 9:09
  • unrelated to fx, removed the tag Commented Dec 26, 2019 at 11:49

2 Answers 2

1

This may help you

You need to change file path as per your file location

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;

public class Test {

public static void main(String[] args) throws IOException {

    BufferedReader bufferedReader = new BufferedReader(new FileReader("C:\\Users\\everestek22\\Desktop\\Invoice.txt"));
    String[] strArray = 
    bufferedReader.lines().map(String::new).toArray(String[]::new);
    //        String line = bufferedReader.readLine();
    //        while (line != null) {
    //            System.out.println(line);
    //            line = bufferedReader.readLine();
    //            String[] strArray = bufferedReader.lines().map(String::new).toArray(String[]::new);
    //        }
    bufferedReader.close();

    for (String s : strArray) {
        System.out.println(s);
    }

}
}
Sign up to request clarification or add additional context in comments.

Comments

0

Don't bother trying to read specific lines from the file, just read all the lines from the file, then lookup your question by index in the resultant list.

List<String> questions = Files.readAllLines(
    Paths.get("<your file path>")
);

Then you could choose a question at random:

Random random = new Random(42);
int randomQuestionIndex = random.nextInt(questions.size());
String randomQuestion = questions.get(randomQuestionIndex);

Using 42 as the seed to the random number generator makes the random sequence repeatable, which is good for testing. To have it truly psuedo-random, then remove the seed (e.g. just new Random());

If the structure of the data you wish to read is complex, then use a helper library such as Jackson to store and retrieve the data as serialized JSON objects. If it is even more complex, then a database can be used.

If you have a really large file and you know the position in the file of each specific thing you wish to read, then you can use a random access file for lookup. For example, if the all the questions in the file are exactly the same length and you know how many questions are stored there, then a random access file might be used fairly easily. But, from your description of what you need to do, this is likely not the case, and the simpler solution of reading everything rather than using a random access file is better.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.