Results 1 to 16 of 16
Like Tree2Likes
  • 1 Post By JBelg
  • 1 Post By Tolls

Thread: Error in "main"

  1. #1
    KevinNguyen is offline Member
    Join Date
    Oct 2012
    Posts
    18
    Rep Power
    0

    Default 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();
    	}	
    }
    Java 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 hnh nổi tiếng Shrek của hng no sản xuất?","Walt Disney","Pixar","DreamWorks Animation","Madhouse","C",11,2));
    		MCQ.add(new MultiChoiceQuestion("Số tiếp tục trong dy số dưới đy l số no?: 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","Khc","D",7,1));
    		MCQ.add(new MultiChoiceQuestion("Bạn c biết c khoảng bao nhiu ngn ngữ c trn tri đất?","3500","4700","6500","7300","C",11,2));
    		MCQ.add(new MultiChoiceQuestion("Trong 4 con vật sau đy, th con no c đặc điểm t giống nhất với 3 con cn lại?","Ch","Voi","Mo","Ra","D",8,1));
    		ObjectOutputStream out1 = new ObjectOutputStream(file1);
    		out1.writeObject(MCQ);
    		out1.close();
    		
    		Q.add(new Question("Sng no c một bờ ở chu u, một bờ ở chu ?","Sng U-ran",7,2));
    		Q.add(new Question("Hnh tinh no 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 nhiu con chuột?","12",3,3));
    		Q.add(new Question("Đồng bằng no lớn nhất thế giới?","Amazon",4,4));
    		Q.add(new Question("Chữ ci cuối cng trong bảng chữ ci 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);
    	}
    }
    Error here
    Java 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)
    Please help me :). Thanks
    Last edited by KevinNguyen; 10-31-2012 at 03:12 PM.

  2. #2
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,820
    Rep Power
    19

    Default 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.

    ** This space for rent **

  3. #3
    JBelg is offline Member
    Join Date
    Oct 2012
    Posts
    36
    Rep Power
    0

    Default 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
    Tolls likes this.

  4. #4
    KevinNguyen is offline Member
    Join Date
    Oct 2012
    Posts
    18
    Rep Power
    0

    Default Re: Error in "main"

    Quote Originally Posted by Tolls View Post
    Looks like you wrote out the object with one version of the class and are reading in with another?
    I wrote some samples first then read them into ArrayList. The first time reading is OK, but the second time I run the program, these errors appeared. Also, I'm learning how to write and read file, so if there are anything wrong, please teach me :)

  5. #5
    KevinNguyen is offline Member
    Join Date
    Oct 2012
    Posts
    18
    Rep Power
    0

    Default Re: Error in "main"

    Quote Originally Posted by JBelg View Post
    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
    Hmm, in my teacher presentation slide, she doesn't add the serial. Let me try it then I will report.

  6. #6
    KevinNguyen is offline Member
    Join Date
    Oct 2012
    Posts
    18
    Rep Power
    0

    Default 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

  7. #7
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,820
    Rep Power
    19

    Default Re: Error in "main"

    Please don't change the original post like that.
    It makes large chunks of the thread nonsensical.
    DarrylBurke likes this.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  8. #8
    KevinNguyen is offline Member
    Join Date
    Oct 2012
    Posts
    18
    Rep Power
    0

    Default Re: Error in "main"

    Ah yes, sorry :), just want to make the thread clean. So, do you know what is my fault?

  9. #9
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,820
    Rep Power
    19

    Default 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.

    ** This space for rent **

  10. #10
    KevinNguyen is offline Member
    Join Date
    Oct 2012
    Posts
    18
    Rep Power
    0

    Default Re: Error in "main"

    Quote Originally Posted by Tolls View Post
    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.
    Yes, I know this problem too, but I don't know how to fix it after although send the Sc.next() or Sc.nextLine()

  11. #11
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,820
    Rep Power
    19

    Default 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.

    ** This space for rent **

  12. #12
    KevinNguyen is offline Member
    Join Date
    Oct 2012
    Posts
    18
    Rep Power
    0

    Default 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

  13. #13
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,820
    Rep Power
    19

    Default 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.

    ** This space for rent **

  14. #14
    KevinNguyen is offline Member
    Join Date
    Oct 2012
    Posts
    18
    Rep Power
    0

    Default 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 hnh nổi tiếng Shrek của hng no sản xuất?","Walt Disney","Pixar","DreamWorks Animation","Madhouse","C",11,2));
            MCQ.add(new MultiChoiceQuestion("Số tiếp tục trong dy số dưới đy l số no?: 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","Khc","D",7,1));
            MCQ.add(new MultiChoiceQuestion("Bạn c biết c khoảng bao nhiu ngn ngữ c trn tri đất?","3500","4700","6500","7300","C",11,2));
            MCQ.add(new MultiChoiceQuestion("Trong 4 con vật sau đy, th con no c đặc điểm t giống nhất với 3 con cn lại?","Ch","Voi","Mo","Ra","D",8,1));
            ObjectOutputStream out1 = new ObjectOutputStream(file1);
            out1.writeObject(MCQ);
            out1.close();
             
            Q.add(new Question("Sng no c một bờ ở chu u, một bờ ở chu ?","Sng U-ran",7,2));
            Q.add(new Question("Hnh tinh no 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 nhiu con chuột?","12",3,3));
            Q.add(new Question("Đồng bằng no lớn nhất thế giới?","Amazon",4,4));
            Q.add(new Question("Chữ ci cuối cng trong bảng chữ ci 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)

  15. #15
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,820
    Rep Power
    19

    Default 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.

    ** This space for rent **

  16. #16
    KevinNguyen is offline Member
    Join Date
    Oct 2012
    Posts
    18
    Rep Power
    0

    Default 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();
        }
    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)
    Haha, this drives me crazy....

    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 :D
    Last edited by KevinNguyen; 11-02-2012 at 06:47 PM.

Similar Threads

  1. Replies: 2
    Last Post: 06-20-2012, 12:18 AM
  2. Replies: 6
    Last Post: 01-21-2012, 05:59 AM
  3. Replies: 5
    Last Post: 06-22-2010, 03:51 PM
  4. Replies: 1
    Last Post: 11-20-2009, 07:58 PM
  5. Replies: 1
    Last Post: 08-07-2007, 07:10 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •