WorkClass
import java.io.*;
import java.util.*;
public class WorkClass {
public static WorkClass useTextFile(String file)throws IOException {
String quiz_name, category, question, answer, next;
int num_questions, max_num_questions, num_chances, num_answers;
Holder HOLD;
WorkClass WC;
FileReader in = new FileReader(file);
Scanner trivia_in = new Scanner(in);
quiz_name = trivia_in.nextLine();
num_questions = trivia_in.nextInt();
num_chances = trivia_in.nextInt();
next = trivia_in.nextLine();
// Instantiate the Trivia Game.
HOLD = new Holder(quiz_name, max_num_questions, num_chances);
// Scan in and set up the questions and answers.
for (int i = 1; i <= num_questions; i++)
{
category = trivia_in.nextLine();
question = trivia_in.nextLine();
num_answers = trivia_in.nextInt();
next = trivia_in.nextLine();
HOLD = new Holder(category, question, num_answers);
for (int j = 1; j <= num_answers; j++)
{
answer = trivia_in.nextLine();
HOLD.save_answer(answer);
}
HOLD.insertQuestion(HOLD);
}
return WC;
}
}
Holder Class
public class Holder {
String quiz_name, category, question, answer;
int max_num_questions, num_chances, num_answers, remaining_chances, num_correct = 0, num_incorrect = 0, curr_num_questions = 0;
Holder[] questions;
boolean[] correct;
StringLogInterface SLI;
public Holder(String category, String question, int max_numbers) {
this.category = category;
this.question = question;
SLI = new StringLog("Trivia", max_numbers);
}
public Holder(String quiz_name, int max_num_questions, int num_chances) {
this.quiz_name = quiz_name;
this.max_num_questions = max_num_questions;
this.num_chances = num_chances;
remaining_chances = num_chances;
questions = new Holder[max_num_questions];
correct = new boolean[max_num_questions];
}
public String get_category() {
return category;
}
public String get_question() {
return question;
}
public boolean try_answer(String ANS) {
return SLI.contains(ANS);
}
public void save_answer(String ANS) {
SLI.insert(ANS);
}
public String get_quiz_name() {
return quiz_name;
}
public int get_num_chances() {
return num_chances;
}
public int get_remaining_chances() {
return remaining_chances;
}
public int get_num_correct() {
return num_correct;
}
public int get_num_incorrect() {
return num_incorrect;
}
public int get_curr_num_questions() {
return curr_num_questions;
}
public Holder get_trivia_question(int QUESTNUMBER) {
return questions[QUESTNUMBER - 1];
}
public boolean is_answered(int QUESTNUMBER) {
return correct[QUESTNUMBER - 1];
}
public boolean is_over() {
return (num_correct == curr_num_questions)
||
(remaining_chances <= 0);
}
public void insertQuestion(Holder question) {
questions[curr_num_questions] = question;
correct[curr_num_questions] = false;
curr_num_questions = curr_num_questions + 1;
}
public void correct_answer(int QUESTNUMBER) {
correct[QUESTNUMBER - 1] = true;
num_correct = num_correct + 1;
remaining_chances = remaining_chances - 1;
}
public void incorrect_answer() {
num_incorrect = num_incorrect + 1;
remaining_chances = remaining_chances - 1;
}
}
I am trying to add the question taken from the .txt file to the array of questions I have setup in the Holder class but I am getting an uninitalized error on
return WC
Which I can understand because I am never setting WC to anything but I can't think of another solution that doesn't involve adding in another class which I can't due do to project limitations. Any suggestions would be greatly appreciated.

New Topic/Question
Reply



MultiQuote




|