I've been working on this for quite a while. Everything works except for writing and reading my Vector<Question> questionList to and from my binary file. The file is created as it should be and the rest of the program works ok, but I'm not sure what I'm doing wrong with my object reader and writer.
Thanks for any help.
/**
* This is a small trivia game administration program which
* allows the admin to view the questions, and add or delete as neccessary.
*/
import java.util.Scanner;
import java.util.Vector;
import java.io.*;
public class GameAdmin implements Serializable {
/**
*
*/
private static final long serialVersionUID = 2;
Vector<Question> questionList;
int numQuestions = 0;
public GameAdmin() {
// read from dat file if it exists
/**
* @param args
*/
Vector<Question> vector = new Vector<Question>();
questionList = vector;
File file = new File("questions.dat");
String fileName = "questions.dat";
if (!file.exists()) {
writeQuestionList(questionList, fileName);
}
Question q;
try {
ObjectInputStream binaryInputStream = new ObjectInputStream(
new FileInputStream(file));
try {
while (binaryInputStream.available() != 0) {
Vector<Question> readObject = (Vector<Question>) binaryInputStream
.readObject();
questionList = readObject;
System.out.println("Question list size = "
+ questionList.size());
}
} catch (Exception e) {
System.out.println(e.toString());
System.out.println("No questions are presnt in the file");
} finally {
binaryInputStream.close();
}
} catch (FileNotFoundException e) {
System.out.println("No question file found.");
} catch (IOException e) {
System.out.println("IOException.");
}
// write questions to file
int guess = 0;
String tempQuestion = null;
String tempAnswer = null;
int tempValue = 0;
// Make menu
while (guess != 4) {
System.out.println("Menu");
System.out.println("Please Choose a number");
System.out.println("1. Print Questions");
System.out.println("2. Add Question");
System.out.println("3. Delete Question");
System.out.println("4. End Program");
Scanner scanner = new Scanner(System.in);
guess = scanner.nextInt();
// print out questions
if (guess == 1) {
if (questionList.size() <= 0) {
System.out.println("The question list is empty");
} else {
for (int j = 0; j < questionList.size(); j++) {
Question que = questionList.get(j);
System.out.println("Question " + j + ". "
+ que.toString());
}
}
}
// add question
if (guess == 2) {
System.out.println("Enter a question");
scanner.nextLine(); // had to add this because scanner skips
// line otherwise (Windows 7 running
// eclipse)
tempQuestion = scanner.nextLine();
System.out.println("Enter the answer to the question");
tempAnswer = scanner.nextLine();
System.out.println("Enter the value 1 to 3");
tempValue = scanner.nextInt();
q = new Question(tempQuestion, tempAnswer, tempValue);
questionList.add(q);
numQuestions++;
}
// delete question
if (guess == 3) {
System.out
.println("What question number do you want to delete");
int j = scanner.nextInt();
questionList.remove(j);
numQuestions--;
}
// end program
if (guess == 4) {
writeQuestionList(questionList, fileName);
System.out.println("End of program.");
System.exit(0);
}
}
}
public String writeQuestionList(Vector<Question> questionList,
String fileName) {
try {
// File data = new File(fileName);
ObjectOutputStream out = new ObjectOutputStream(
new FileOutputStream("questions.dat"));
out.writeObject(questionList);
return "Question list saved to file";
} catch (Exception e) {
return "Error saving to " + fileName + ". Question not saved.\n"
+ e;
}
}
public static void main(String[] args) {
// TODO Auto-generated method stub
GameAdmin g = new GameAdmin();
}
}// end GameAdmin class
/**
*
* This class is to create questions for a trivia game
*
*/
public class Question {
private String question;
private String answer;
private int value;
public String getQuestion() {
return question;
}
public String getAnswer() {
return answer;
}
public int getValue() {
return value;
}
public Question(String q, String a, int v) {
question = q;
answer = a;
value = v;
}
public Question() {
question = "";
answer = "";
value = 0;
}
@Override
public String toString() {
return "question= " + question + ", answer= " + answer + ", value= "
+ value;
}
}