简体   繁体   中英

How do I loop through a text file in Java adding a 7 line object to an array of objects?

Im working on a project that is going to be a trivia game. I need to create a method that loops through a text file and creates an object then adds it to an ArrayList. Every 7 lines of the file is a new object. The object itself is a question containing things such as possible answers etc.

My Object Constructor:

 public Question(String question, int possibleAnswers, String[] answers, int correctAnswer) {
    this.question = question;
    this.possibleAnswers = possibleAnswers;
    this.answers = answers;
    this.correctAnswer = correctAnswer;
}

I need to loop through a file that has the question info like such:

Which of the following is not a programming language?
4
Python
Java
PHP
SQL
4

Im confused about how I should generate each object while looping through this is as far as I have gotten on the method:

public static ArrayList<Object> createQuestions(String filename) throws IOException {

    ArrayList<Question> questionObj = new ArrayList<Question>();
    Question questionArray[] = null;

    Scanner fileReader = new Scanner(new File(filename));
    while (fileReader.hasNext()) {
        for (int i = 0; i <= 10; i++) {
            questionArray[i] = new Question(); 

        }
    }
    fileReader.close();

I would break the problem down into methods like: createQuestions , buildQuestion , and getPossibleAnswers . That's basically what you'll be doing so just represent it with code. Like so ( left the fun parts out since you're a student and this is an assignment :) ):

public static String[] getPossibleAnswers(Scanner scanner, int possibleAnswers) {
    // Get the possible answers by using the passed in int and scanner...
}

public static Question buildQuestion(Scanner scanner) {
    // Step through the first couple lines building your new question
    // and get q.question and q.possibleAnswers...
    // Now get the possible answers using the helper method
    q.answers = getPossibleAnswers(scanner, q.possibleAnswers);
    // Finish up
    return q;
}

public static ArrayList<Question> createQuestions(String filename) throws IOException {

    ArrayList<Question> questions = new ArrayList<>();

    Scanner fileReader = new Scanner(new File(filename));
    // Every time the reader has a next, it's another question
    while (fileReader.hasNext()) {
        // Here's a question
        questions.add(buildQuestion(fileReader));
    }
    fileReader.close();
    return questions;
}

If you really want to treat the file like it's indexed then here's a way. As you read the file, do so in chunks of 7 lines adding them to a string array. Sort of like you have.

while (fileReader.hasNext()) {
    String[] lines = new String[7];
    for (int i = 0; i < 7 && fileReader.hasNext(); i++) {
        // Fill the array with the lines that make up the current question
    }
    questions.add(new Question(lines);
}

Now, provide the constructor to match or have a builder that takes a string array and returns a question. ( I would opt for the builder ) Either way, there you'll move from the array to the actual values knowing that some are actually integers.

A function for your refer. But a recommend that you should save your question list in an excel file for well formatted and easy to read also maintain.

public List<Question> createQuestion(String fileName){
        BufferedReader br = new BufferedReader(new FileReader(new File("")));
        String line = null;
        int lineIndex  = 0;
        Question q = null;
        List<Question> listQ = new ArrayList<>();
        String[] answers = new String[4];
        while ((line = br.readLine()) != null){     
            if (lineIndex >= 7){
                lineIndex = 0;
                listQ.add(q);
            }
            switch (lineIndex){
            case 0:
                q = new Question();
                q.setQuestion = br.readLine();
                lineIndex ++;
                break;
            case 1:             
                q.setpossibleAnswers = br.readLine();
                lineIndex ++;
                break;
            case 2:
                answers[0] = br.readLine();
                lineIndex ++;
                break;
            case 3:
                answers[1] = br.readLine();
                lineIndex ++;
                break;
            case 4:
                answers[2] = br.readLine();
                lineIndex ++;
                break;
            case 5:
                answers[3] = br.readLine();
                q.setAnswer(answers);
                lineIndex ++;               
                break;
            case 6:
                q.correctAnswer = br.readLine();
                lineIndex ++;
                break;
            default:
                break;
            }       
        }
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM