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;
	}

}

Recommended Answers

All 6 Replies

Vector<Question> questionList;
...
out.writeObject(questionList);

This will need the Question class to be declared as " implements Serializable ".
As it only has Strings and an int, there's no need to do anything else

Thanks for the help, I hadn't had time to work on it again until today. I did as you suggested, but it still gets to line 38 above "while(binaryInputStream.available() != 0 )"

and skips to "binaryInputStream.close()" I still don't know if it isn't writing anything to the file or if it won't read from it, or both.

/**
 * 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> readObject2 = (Vector<Question>) binaryInputStream.readObject();
					//Vector<Question> readObject = readObject2;
					questionList = readObject2;
					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();

	}
}


import java.io.Serializable;



public class Question implements Serializable {
	
		/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
		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  ;
}


}

I don't know why you have that while loop anyway. Just write/read with

ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream("questions.dat"));
out.writeObject(questionList);

and

ObjectInputStream in = new ObjectInStream(new FileInputStream("questions.dat"));
Vector<Question> questionList = (Vector<Question>) in.readObject();

plus some try/catch/close boilerplate

Thanks a lot, I got rid of the loop, and used the syntax you suggested. I took the typecast off of questionList to keep it non-local, and I'll look at boilerplate, we just learned about try/catch, I didn't know you could consolidate it.

Thanks again

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.