Home > database >  Issues saving data from a text file into an array list
Issues saving data from a text file into an array list

Time:11-09

Hello to anyone reading this, I've spent a couple hours now trying to figure out the root of a problem I've been having with an array list in a JavaFX trivia program I'm writing. I've finally narrowed down where the issue is, I just can't figure out what's causing it. Basically, I have a while loop that keeps reading lines of code until it reaches the end, and that works. At the end of each loop, I add the data I read to an ArrayList in the form of an object, that holds the data I read. It works, as when I print one of the parameters from the object at each index in the array list, it works. However, the moment I step outside of the while loop, every single index of the ArrayList holds the exact same data, and it is always the final question I saved for.

Here is the code:

        while ((line = questionStream.readLine()) != null) {

            // The question
            String inquiry = line;

            // The first possible response
            line = questionStream.readLine();
            String[] responses = new String[4];
            responses[0] = line;

            // The second possible response
            line = questionStream.readLine();
            responses[1] = line;

            // The third possible response
            line = questionStream.readLine();
            responses[2] = line;

            // The fourth possible response
            line = questionStream.readLine();
            responses[3] = line;

            // The fact to display once the question has been answered
            line = questionStream.readLine();
            String fact = line;

            // Space in between questions
            questionStream.readLine();

            // Adding the question
            questions.add(new Question(inquiry, responses, fact));
            Console.print(questions.get(temp).getInquiry());
            temp  ;
        }

        questionStream.close();
        
        for (int i = 0; i < questions.size(); i  ) {
        Console.print(questions.get(i).getInquiry());
        }

And the output is as follows:

How many countries border France?
What sea creature has 3 hearts?
The Simpsons is the longest running tv series. What is the name of the janitor?
What was the product of the first ever TV advertisement?
What TV series features a reference to or a picture of Superman in almost every episode?
How many castaways were on Gilligans Island?
How many castaways were on Gilligans Island?
How many castaways were on Gilligans Island?
How many castaways were on Gilligans Island?
How many castaways were on Gilligans Island?
How many castaways were on Gilligans Island?
How many castaways were on Gilligans Island?

So I'm really confused how the ArrayList holds all of the proper data perfectly, right until it exits the while loop, where it then only ever holds whatever the final question is in the text file.

Thanks for the help!

CodePudding user response:

The problem is that you are not using instance variables in your Question class but class variables. That means your Question class should look like this in order to work as expected:

package trivia.questions;
 
public class Question {
 
    // Data fields, don't use static!
    private String inquiry;
    private String[] answers;
    private String fact;
    private String correctAnswer;
 
... 
    /**
     * Overloaded constructor
     * 
     * @param inquiry the question
     * @param answers the answers
     * @param fact    the cool fact for the correct answer
     */
 
    public Question(String inquiry, String[] answers, String fact) {
        this.inquiry = inquiry;
        this.answers = answers;
        this.fact = fact;
     }

Using static for the variables will make that this variable is shared between all instance of your Question variable.

In each iteration you print out the current state which seem to be okay but after you finished you print out multiple time the same state which is shared between your different copies of class Question.

This is one major concept in object oriented programing in Java.

  • Related