Results 1 to 16 of 16
Thread: Error in "main"
- 10-30-2012, 04:12 PM #1
Member
- Join Date
- Oct 2012
- Posts
- 18
- Rep Power
- 0
Error in "main"
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).
Java 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(); } }Java 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 hereJava Code: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 :). ThanksJava Code: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)
Last edited by KevinNguyen; 10-31-2012 at 03:12 PM.
- 10-30-2012, 04:17 PM #2
Moderator
- Join Date
- Apr 2009
- Posts
- 10,481
- Rep Power
- 16
Re: Error in "main"
Looks like you wrote out the object with one version of the class and are reading in with another?
Please do not ask for code as refusal often offends.
- 10-30-2012, 04:24 PM #3
Member
- Join Date
- Oct 2012
- Posts
- 32
- Rep Power
- 0
Re: Error in "main"
The class that is serialized doesn't equal the serial of the local class.
This is mainly because you didn't add your own serial for the class so Java hashes most of your functions and creates a serial out of that, so if you change even the slightest detail, the serial will change and will not match. The serial will also differ from compiler to compiler, so it's highly recommended to add your own serial, instead of letting the compiler make one for you.
Add serials to your class, the class you're getting an error with should have a serial of: 8116156216592075411
- 10-30-2012, 04:28 PM #4
Member
- Join Date
- Oct 2012
- Posts
- 18
- Rep Power
- 0
- 10-30-2012, 04:38 PM #5
Member
- Join Date
- Oct 2012
- Posts
- 18
- Rep Power
- 0
- 10-31-2012, 03:03 PM #6
Member
- Join Date
- Oct 2012
- Posts
- 18
- Rep Power
- 0
Re: Error in "main"
OK, updated CODE, so there is an error with the input. Please help me on this error and explain please cause I faced this error so many times, fixed with lucky but don't know how I fix it. Thanks
- 10-31-2012, 03:57 PM #7
Moderator
- Join Date
- Apr 2009
- Posts
- 10,481
- Rep Power
- 16
Re: Error in "main"
Please don't change the original post like that.
It makes large chunks of the thread nonsensical.Please do not ask for code as refusal often offends.
- 10-31-2012, 04:08 PM #8
Member
- Join Date
- Oct 2012
- Posts
- 18
- Rep Power
- 0
Re: Error in "main"
Ah yes, sorry :), just want to make the thread clean. So, do you know what is my fault?
- 10-31-2012, 05:27 PM #9
Moderator
- Join Date
- Apr 2009
- Posts
- 10,481
- Rep Power
- 16
Re: Error in "main"
You've got multiple Scanners reading the Sys.in stream.
Now, I haven't gone through your code, but it's quite possible that they are clashing.
Scanner has a buffer, and if one Scanner has consumed the data before the one you really want to consume the data then you might hit problems.
This is likely to be a bit random as well.Please do not ask for code as refusal often offends.
- 10-31-2012, 05:50 PM #10
Member
- Join Date
- Oct 2012
- Posts
- 18
- Rep Power
- 0
- 10-31-2012, 06:21 PM #11
Moderator
- Join Date
- Apr 2009
- Posts
- 10,481
- Rep Power
- 16
Re: Error in "main"
I was going to suggest trying to close each of them when finished, but you seem to have done that...apart from the one in main().
I'd close that one as well.
The other option is to have a single Scanner that you access.Please do not ask for code as refusal often offends.
- 11-01-2012, 01:35 PM #12
Member
- Join Date
- Oct 2012
- Posts
- 18
- Rep Power
- 0
Re: Error in "main"
Hmm, so can you tell me what is the solution for my case cause I don't know how to fix it in the past, just lucky
- 11-01-2012, 02:36 PM #13
Moderator
- Join Date
- Apr 2009
- Posts
- 10,481
- Rep Power
- 16
Re: Error in "main"
Close the Scanner.
You;re doing it everywhere else except in main().
Once you have the 'selection' you may as well call close() on the scanner object.Please do not ask for code as refusal often offends.
- 11-02-2012, 03:19 PM #14
Member
- Join Date
- Oct 2012
- Posts
- 18
- Rep Power
- 0
Re: Error in "main"
Can't solve by closing Scanner too. I even close Scanner each case but it does not seem to be fixed.
Java Code:package QuestionDatabaseManagement; import java.util.*; import java.io.*; public class QuestionManagement { public static void main(String[] args) throws 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(); int s, s1; do { Scanner Sc = new Scanner(System.in); System.out.println("***************************************************************************************"); System.out.println("Please select one: "); System.out.println("\t1. Add a multiple 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("\t5. 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.print("Enter your choice: "); s = Sc.nextInt(); System.out.println("***************************************************************************************"); switch (s) { case 1: MultiChoiceQuestion M = new MultiChoiceQuestion(); M.input(); MCQ.add(M); Sc.close(); break; case 2: Question Que = new Question(); Que.input(); Q.add(Que); Sc.close(); break; case 3: Sc.close(); break; case 4: Sc.close(); break; case 5: Sc.close(); break; case 6: Sc.close(); break; case 7: for (int i = 0; i < MCQ.size(); i++) MCQ.get(i).print(); Sc.close(); break; case 8: for (int i = 0; i < Q.size(); i++) Q.get(i).print(); Sc.close(); break; case 9: Sc.close(); break; default: break; } Sc.close(); } while (s>0 && s<10); } }Java Code: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.nextInt(Scanner.java:2160) at java.util.Scanner.nextInt(Scanner.java:2119) at QuestionDatabaseManagement.QuestionManagement.main(QuestionManagement.java:60)
- 11-02-2012, 03:48 PM #15
Moderator
- Join Date
- Apr 2009
- Posts
- 10,481
- Rep Power
- 16
Re: Error in "main"
No, close it as soon as you have that int.
Java Code:s = Sc.getInt(); Sc.close(); switch (s) etc etc
Please do not ask for code as refusal often offends.
- 11-02-2012, 05:29 PM #16
Member
- Join Date
- Oct 2012
- Posts
- 18
- Rep Power
- 0
Re: Error in "main"
So after close it, I face error in the MultiChoiceQuestion class
Java Code:public void input() { Scanner Sc = new Scanner(System.in); System.out.print("Enter question: "); String Q = Sc.nextLine(); [B][I][U]System.out.print("Enter option A: ");[/U][/I][/B] 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(); }Haha, this drives me crazy....Java Code:Exception in thread "main" java.util.NoSuchElementException: No line found at java.util.Scanner.nextLine(Unknown Source) at Assignment.MultiChoiceQuestion.input(MultiChoiceQuestion.java:79) at Assignment.QuestionManagement.main(QuestionManagement.java:66)
P/S: if you use TeamViewer, please consider pm me through Yahoo silveraero_light at 9 to 10 o'clock GMT+7 and teach me about this issue :DLast edited by KevinNguyen; 11-02-2012 at 06:47 PM.
Similar Threads
-
"Exception in thread "main" java.lang.NoSuchMethodError: main"
By isnkumar in forum New To JavaReplies: 2Last Post: 06-20-2012, 12:18 AM -
Error:Exception in thread "main" java.lang.NoSuchMethodError:main
By jerry jessie in forum New To JavaReplies: 6Last Post: 01-21-2012, 05:59 AM -
Question about error "Exception in thread "main" java.lang.NoSuchMethodError: main
By ferdzz in forum New To JavaReplies: 5Last Post: 06-22-2010, 03:51 PM -
Runtime error "Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 0
By shantimudigonda in forum New To JavaReplies: 1Last Post: 11-20-2009, 07:58 PM -
ERROR: Exception in thread "main" java.lang.NoSuchMethodError: main
By barney in forum New To JavaReplies: 1Last Post: 08-07-2007, 07:10 AM


2Likes
LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks