Hi, I wanna create simple application to manage data for a gameshow. Data is multiple choice question and only question.
I get errors when trying to read data to ArrayList using FileInputStream the second time (the first time is fine).
Code:package QuestionDatabaseManagement;
import java.io.*;
import java.util.*;
public class MultiChoiceQuestion extends Question implements Serializable {
private static final long serialVersionUID = 4050737259583241901L;
private String OptA, OptB, OptC, OptD;
public String getOptA() {
return OptA;
}
public void setOptA(String optA) {
if (optA.length()>0)
OptA = optA;
}
public String getOptB() {
return OptB;
}
public void setOptB(String optB) {
if (optB.length()>0)
OptB = optB;
}
public String getOptC() {
return OptC;
}
public void setOptC(String optC) {
if (optC.length()>0)
OptC = optC;
}
public String getOptD() {
return OptD;
}
public void setOptD(String optD) {
if (optD.length()>0)
OptD = optD;
}
public MultiChoiceQuestion(String ques, String ans, String optA,
String optB, String optC, String optD, int type, int dif) {
super(ques, ans, type, dif);
OptA = optA;
OptB = optB;
OptC = optC;
OptD = optD;
}
public MultiChoiceQuestion() {
this("","","","","","",0,0);
}
public void print() {
System.out.println("Type: " + Category[getType()]);
System.out.println("Difficulty: " + Dif[getDif()]);
System.out.println("Question: " + getQues());
System.out.println("\tA. " + getOptA());
System.out.println("\tB. " + getOptB());
System.out.println("\tC. " + getOptC());
System.out.println("\tD. " + getOptD());
System.out.println("Answer: " + getAns());
}
public void input() {
Scanner Sc = new Scanner(System.in);
System.out.print("Enter question: ");
String Q = Sc.nextLine();
System.out.print("Enter option A: ");
String Oa = Sc.nextLine();
System.out.print("Enter option B: ");
String Ob = Sc.nextLine();
System.out.print("Enter option C: ");
String Oc = Sc.nextLine();
System.out.print("Enter option D: ");
String Od = Sc.nextLine();
System.out.print("Enter answer: ");
String A = Sc.nextLine();
System.out.print("Enter type: ");
int T = Sc.nextInt();
System.out.print("Enter difficulty: ");
int D = Sc.nextInt();
setQues(Q);
setAns(A);
setOptA(Oa);
setOptB(Ob);
setOptC(Oc);
setOptD(Od);
setType(T);
setDif(D);
Sc.close();
}
}
Code:package QuestionDatabaseManagement;
import java.io.*;
import java.util.*;
public class Question implements Serializable{
private static final long serialVersionUID = -1116686755757636504L;
protected String ques, ans;
protected int type; //0:NOTSET(only use to initial beginning value)
//1:MATH|2.PHYSICS|3.ENG|4.LITERATURES|5.CHE|6. ASTRO
//7. GEO|8. BIO|9: HIS|10: LAW|11: ARTS&CULTURE
//12.UNDEFINED
protected String[] Category = {"NOTSET","MATH","PHY","ENG",
"LIT","CHE","ASTRO","GEO","BIO","HIS","LAW","ARTS & CULTURE","UNDEFINED"};
protected int dif; //0: NOTSET
//1: EASY|2: MEDIUM|3: HARD
//4: UNDIFINED
protected String[] Dif = {"NOTSET","EASY","MEDIUM","HARD","UNDEFINED"};
// The last element of these arrays must always be "UNDEFINED"
// also the last value of the <type> and <dif> variables should mean "undefined value"
private static int count = 0;
public String getQues() {
return ques;
}
public void setQues(String ques) {
this.ques = ques;
}
public String getAns() {
return ans;
}
public void setAns(String ans) {
if (ans.length()>0)
this.ans = ans;
}
public int getType() {
return type;
}
public void setType(int type) {
if (type>0 && type<Category.length)
this.type = type;
else this.type = Category.length;
}
public int getCount() {
return count;
}
public int getDif() {
return dif;
}
public void setDif(int dif) {
this.dif = dif;
}
public Question(String ques, String ans, int type, int dif) {
this.ques = ques;
this.ans = ans;
this.type = type;
this.dif = dif;
count++;
}
public Question() {
this("","",0,0);
}
public void print() {
System.out.println("Type: " + Category[getType()]);
System.out.println("Dificulty: " + Dif[getDif()]);
System.out.println("Question: " + getQues());
System.out.println("Answer: " + getAns());
}
public void input() {
Scanner Sc = new Scanner(System.in);
System.out.println("Enter question: ");
String Q = Sc.nextLine();
System.out.println("Enter answer: ");
String A = Sc.nextLine();
System.out.println("Enter type: ");
int T = Sc.nextInt();
System.out.println("Enter difficulty: ");
int D = Sc.nextInt();
setType(T);
setDif(D);
setQues(Q);
setAns(A);
Sc.close();
}
}
Error hereCode:package QuestionDatabaseManagement;
import java.util.*;
import java.io.*;
public class QuestionManagement {
public static void main(String[] args) throws Exception, IOException, ClassNotFoundException {
ArrayList<MultiChoiceQuestion> MCQ = new ArrayList<MultiChoiceQuestion>();
ArrayList<Question> Q = new ArrayList<Question>();
/*
FileOutputStream file1 = new FileOutputStream("MCQ.dat");
FileOutputStream file2 = new FileOutputStream("Q.dat");
MCQ.add(new MultiChoiceQuestion("Bộ phim hoạt hình nổi tiếng Shrek của hãng nào sản xuất?","Walt Disney","Pixar","DreamWorks Animation","Madhouse","C",11,2));
MCQ.add(new MultiChoiceQuestion("Số tiếp tục trong dãy số dưới đây là số nào?: 6 20 62 188","564","566","568","600","B",1,1));
MCQ.add(new MultiChoiceQuestion("Nước ở đâu rộng nhất Thế Giới ?","Mỹ","Nga","Trung Quốc","Khác","D",7,1));
MCQ.add(new MultiChoiceQuestion("Bạn có biết có khoảng bao nhiêu ngôn ngữ có trên trái đất?","3500","4700","6500","7300","C",11,2));
MCQ.add(new MultiChoiceQuestion("Trong 4 con vật sau đây, thì con nào có đặc điểm ít giống nhất với 3 con còn lại?","Chó","Voi","Mèo","Rùa","D",8,1));
ObjectOutputStream out1 = new ObjectOutputStream(file1);
out1.writeObject(MCQ);
out1.close();
Q.add(new Question("Sông nào có một bờ ở châu Âu, một bờ ở châu Á?","Sông U-ran",7,2));
Q.add(new Question("Hành tinh nào lớn nhất trong hệ Mặt Trời?","Mộc tinh",6,1));
Q.add(new Question("Trong bức tranh Đám cưới chuột có tất cả bao nhiêu con chuột?","12",3,3));
Q.add(new Question("Đồng bằng nào lớn nhất thế giới?","Amazon",4,4));
Q.add(new Question("Chữ cái cuối cùng trong bảng chữ cái Hy Lạp?","Omega",5,0));
ObjectOutputStream out2 = new ObjectOutputStream(file2);
out2.writeObject(Q);
out2.close();
*/
FileInputStream file1 = new FileInputStream("MCQ.dat");
ObjectInputStream in1 = new ObjectInputStream(file1);
MCQ = (ArrayList)in1.readObject();
in1.close();
FileInputStream file2 = new FileInputStream("Q.dat");
ObjectInputStream in2 = new ObjectInputStream(file2);
Q = (ArrayList)in2.readObject();
in2.close();
byte selection;
do {
Scanner Sc = new Scanner(System.in);
System.out.println("Please select one: ");
System.out.println("\t1. Add a muiltiple choice question");
System.out.println("\t2. Add a question");
System.out.println("\t3. Delete a multiple choice question");
System.out.println("\t4. Delete a question");
System.out.println("\t6. Edit a multiple choice question");
System.out.println("\t6. Edit a question");
System.out.println("\t7. Report: Multiple choice question database");
System.out.println("\t8. Report: Question database");
System.out.println("\t9. Report: All database");
System.out.println("Enter your choice: ");
selection = Sc.nextByte();
switch (selection) {
case 1:
MultiChoiceQuestion M = new MultiChoiceQuestion();
M.input();
MCQ.add(M);
break;
case 2:
Question Que = new Question();
Que.input();
Q.add(Que);
break;
case 3:
case 4:
break;
case 5:
break;
case 6:
for (int i = 0; i < MCQ.size(); i++) MCQ.get(i).print();
break;
case 7:
for (int i = 0; i < Q.size(); i++) Q.get(i).print();
break;
case 8:
break;
default: break;
}
}
while (selection>0 && selection<7);
}
}
Please help me :). ThanksCode:Please select one:
1. Add a muiltiple choice question
2. Add a question
3. Delete a multiple choice question
4. Delete a question
6. Edit a multiple choice question
6. Edit a question
7. Report: Multiple choice question database
8. Report: Question database
9. Report: All database
Enter your choice:
1
Enter question: 1
Enter option A: 1
Enter option B: 1
Enter option C: 1
Enter option D: 1
Enter answer: 1
Enter type: 1
Enter difficulty: 1
Please select one:
1. Add a muiltiple choice question
2. Add a question
3. Delete a multiple choice question
4. Delete a question
6. Edit a multiple choice question
6. Edit a question
7. Report: Multiple choice question database
8. Report: Question database
9. Report: All database
Enter your choice:
Exception in thread "main" java.util.NoSuchElementException
at java.util.Scanner.throwFor(Scanner.java:907)
at java.util.Scanner.next(Scanner.java:1530)
at java.util.Scanner.nextByte(Scanner.java:1924)
at java.util.Scanner.nextByte(Scanner.java:1883)
at QuestionDatabaseManagement.QuestionManagement.main(QuestionManagement.java:58)

