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!