Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trial

Nancy Melucci
Courses Plus Student 36,159 PointsMake a text file of questions into a quiz using Java FileReader
I have an assignment to create an interactive quiz using a text file. A question is formatted like this (it's actually single spaced I had to separate the lines to keep the forum from smushing the whole thing together.):
Which Countries share a border with Switzerland?
4
1)France, Belgium, Spain and Bulgaria
2)France, Italy, Germany and Belgium
3)France, Italy, Germany and Austria
4)France, Spain, Slovenia and Bulgaria
3
The first int is the number of answer options and the second one is the correct answer. I am supposed to create an array list of question objects using a file with 10 questions.
I am presuming that first I need to read the questions as 10 separate objects and form the arraylist. Then the arraylist needs to feed questions to two players.
I think I can do the second task. But I am stumped about how to read a text file into a set of objects. I still struggle with File I/O it is not my strong suit
Any suggestions would be appreciated.
2 Answers

Chris Tvedt
3,795 PointsHi, whipped up som quick code to show you a quick way of doing what you ask.
NOTE! This is NOT good coding practise and should not be viewed as such! This is just a quick prototype from a noob coder to show how to read from file in the syntax that you asked for. I have not made any UI, just a read from file and print to console "app".
My "questions.txt"-file looks like this:
What is H20?
4
1) Cocaine
2) Milk
3) Water
4) Coffee
3
Can this Java-program be written better?
2
1) Nah
2) Hell yes!
2
Who teaches Java at teamtreehouse?
3
1) Craig
2) Susan
3) Jane
1
Which Countries share a border with Switzerland?
4
1) France, Belgium, Spain and Bulgaria
2) France, Italy, Germany and Belgium
3) France, Italy, Germany and Austria
4) France, Spain, Slovenia and Bulgaria
3
The java-file like this:
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Scanner;
class Question {
private String question;
private String[] alternatives;
private int answer;
public Question(String question, String[] alternatives, int answer) {
this.question = question;
this.alternatives = alternatives;
this.answer = answer;
}
public String getQuestion() {
return question;
}
public String[] getAlternatives() {
return alternatives;
}
public int getAnswer() {
return answer;
}
@Override
public String toString() {
String print = question + "\n";
for (String alternative : alternatives) {
print += alternative + "\n";
}
print += "Answer: " + answer + "\n";
return print;
}
}
class Questions {
private ArrayList<Question> questions = new ArrayList<>();
//This should be written more efficiently! This is not good practise! Just a quick prototype.
public Questions() {
try {
FileReader file = new FileReader("questions.txt");
BufferedReader reader = new BufferedReader(file);
Scanner scanner = new Scanner(reader);
String line;
String question = "";
String[] alternatives = null;
int answer = 0;
int numberOfAlternatives = 0;
int counter = 0;
do {
do {
line = scanner.nextLine();
if (line.contains("?")) { //stores the question
question = line;
} else if(counter == 0 && line.length() == 1) { //stores the number of alternatives
numberOfAlternatives = Integer.valueOf(line);
alternatives = new String[numberOfAlternatives];
} else if (line.contains(")")) { //stores the alternatives
alternatives[counter++] = line;
} else if (Character.isDigit(line.indexOf(0)) || counter == numberOfAlternatives) { //Stores the answer
answer = Integer.valueOf(line);
}
} while (answer == 0);
questions.add(new Question(question, alternatives, answer));
numberOfAlternatives = 0;
counter = 0;
answer = 0;
} while (scanner.hasNext());
file.close();
reader.close();
scanner.close();
} catch (IOException e) {
e.printStackTrace();
}
}
public ArrayList<Question> getQuestions() {
return questions;
}
}
public class Quiz {
public static void main(String[] args) {
Questions quiz = new Questions();
for (Question question: quiz.getQuestions()) {
System.out.println(question);
}
}
}
the console printout looks like this:
What is H20?
1) Cocaine
2) Milk
3) Water
4) Coffee
Answer: 3
Can this Java-program be written better?
1) Nah
2) Hell yes!
Answer: 2
Who teaches Java at teamtreehouse?
1) Craig
2) Susan
3) Jane
Answer: 1
Which Countries share a border with Switzerland?
1) France, Belgium, Spain and Bulgaria
2) France, Italy, Germany and Belgium
3) France, Italy, Germany and Austria
4) France, Spain, Slovenia and Bulgaria
Answer: 3
Hope this helps you in some way :-) Upvote if it did please.
Chris

Nancy Melucci
Courses Plus Student 36,159 PointsWill do. The instructor is not a fascist about due dates; I need to pay attention to my day job (><) then I will try to build something using your code to guide me mid-week. I will post an update.

Chris Tvedt
3,795 PointsAwesome, let me know how it went :-)
Nancy Melucci
Courses Plus Student 36,159 PointsNancy Melucci
Courses Plus Student 36,159 PointsThank you so much. I've been trying to become proficient at Java for about three years. I love writing code but when I stumble over stuff like this I feel embarrassed and a little hopeless. I will return to the task in a day or two, and try this out. I appreciate your taking the time for my question.
Chris Tvedt
3,795 PointsChris Tvedt
3,795 PointsNo problem Nancy. Keep at it! Nothing to be embarrassed about, no one know everything. The best thing you can do is to reach out to the community and ask for help.
Please let me know if this worked like you wanted it to and if there are any thing unclear, let me know and i will try my best to help you out. eventhough, i am far from an expert (done java for about a year now).