So I am writing a quiz program that contains an uncertain # of questions in it (in Java) and I am having problems accomplishing the following things:
1) loading quiz question from file and storing in arraylist and accessing it (need help!) 2) correct answer not accepted - gives me error (logic error) 3) not all answer choices are displayed
Here's the code:
import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
public class Quiz {
public void writeFile() {
Question qn = new Question();
try {
PrintWriter out = new PrintWriter("quiz.txt");
out.println(qn.Question);
out.println(qn.numberOfChoices);
qn.answerChoices = new String[qn.numberOfChoices];
for (int i = 0; i < qn.numberOfChoices; i++) {
out.println(qn.answerChoices[i]);
}
out.println(qn.correctAnswer);
out.println(qn.numOfTries);
out.println(qn.numOfCorrectTries);
out.close();
} catch (IOException f) {
System.out.println("Error.");
}
qn.getQuestion();
}
public void readFile() {
File file = new File ("quiz.txt");
boolean exists = file.exists();
Quiz q = new Quiz();
Question a = new Question();
List<String> question = new ArrayList<String>();
String[] answerChoices = a.answerChoices;
try {
if (exists == true) {
Scanner s = new Scanner(file);
a.Question = s.nextLine();
a.numberOfChoices = s.nextInt();
a.answerChoices = new String[a.numberOfChoices];
for (int i = 0; i < a.numberOfChoices; i++) {
a.answerChoices[i] = s.nextLine();
}
s.nextLine();
a.correctAnswer = s.nextInt();
a.numOfTries = s.nextInt();
a.numOfCorrectTries = s.nextInt();
a.getQuestion();
} else {
q.writeFile();
}
} catch (IOException e) {
System.out.println("File not found.");
}
}
public static void main (String[] args) {
Scanner in = new Scanner(System.in);
Quiz qz = new Quiz();
Question b = new Question();
int Selection;
List<String> question = new ArrayList<String>();
System.out.println("Welcome to the Quiz Program! Good luck!");
do {
qz.readFile();
System.out.println("Your answer?: ");
Selection = in.nextInt();
if (Selection == b.correctAnswer) {
b.numOfCorrectTries++;
b.getQuestion();
} else {
b.getQuestion();
}
} while (Selection < b.numberOfChoices);
while (Selection > b.numberOfChoices || Selection < 0) {
System.out.println("Error. Try again");
System.out.println("Your answer?: ");
Selection = in.nextInt();
}
}
}
and the question class:
import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
public class Question {
int correctAnswer;
int numOfTries;
int numOfCorrectTries;
int numberOfChoices;
String Question;
String[] answerChoices;
public Question() {
}
public void getQuestion() {
System.out.println("Question: " + Question);
System.out.println("Answer: ");
for (int i = 0; i < numberOfChoices; i++) {
System.out.println(answerChoices[i]);
}
}
public double getPercentageRight() {
double percentageRight = (numOfCorrectTries / numOfTries) * 100;
percentageRight = Math.round(percentageRight * 100);
percentageRight = percentageRight / 100;
return percentageRight;
}
}
QUIZ.TXT:
How many licks does it take to get to the tootsie roll center of a
tootsie pop?
4
one
two
three
four
2
14
5
What is your name?
3
Arthur, King of the Britons
Sir Lancelot the Brave
Sir Robin the Not-Quite-So-Brave-As-Sir Lancelot
0
14
6
Who's on first?
5
What
Why
Because
Who
I don't know
3
14
7
Which of the following is a terror of the fire swamp?
4
Lightning sand
Flame spurt
R.O.U.S.
All of the above
3
14
4
Who is the all-time greatest pilot?
6
Manfred von Richthofen
Chuck Yeager
Hiraku Sulu
Luke Skywalker
Kara Thrace
Charles Lindbergh
4
14
9
quiz.txtfile?