-3

I'm making a quiz program. First step, I'm taking the quiz questions and their correct answers from the teacher/ user. I have a child class named TrueFalseQuestion which takes the boolean model answer and the string question as a parameter. I've made an array of TrueFalseQuestion type and I'm stuck at this part where I run the code, insert a question and no matter whether the model answer I've inserted is true or false, it's always stored as false when I print it out. HELP? Here's this part of my code:

   System.out.println("How many true or false questions would you like to include in your quiz?");
    int l=s.nextInt();
    TrueFalseQuestion[] qu2= new TrueFalseQuestion[l];
    int x;
    for(x=0;x<l;x++){
        System.out.println("Please insert question "+(x+1)+":\n");
        String Q2=s.next();
        System.out.println("Please insert the correct answer");
        boolean A2=s.nextBoolean();
        qu2[x]=new TrueFalseQuestion(Q2,A2);
        System.out.println(qu2[x].GetCorrectAnswer());


}

EDIT: Here's the TrueFalseQuestion code

public class TrueFalseQuestion extends Question {
   private boolean CorrectB; 

   public TrueFalseQuestion(String qu, boolean b){
         super(qu); 
   } 
   @Override 
   public void GetQuestion() { 
         System.out.println(getMyText()+"\n Is this statement true or false?"); 
   } 
   @Override public String GetAnswer() { 
       System.out.println("Insert Answer: "); 
       boolean MyAnswer=s.nextBoolean(); 
       return Boolean.toString(MyAnswer); 
   }

   @Override public String CheckAnswer() { 
   return Boolean.toString(GetAnswer().equalsIgnoreCase(Boolean.toString(GetCorrectAnswer(‌​))));
    } 
   /** * return the MyAnswer / /* * return the CorrectAnswer / public boolean GetCorrectAnswer() { return CorrectB; } /* * return the MyAnswer */ 

   } 
dkatzel
  • 31,188
  • 3
  • 63
  • 67
  • Probably relevant: http://stackoverflow.com/questions/513832/how-do-i-compare-strings-in-java though you don't show the actual "compare answers" code. – Marc B Nov 10 '14 at 20:11
  • have you use == instead of `.equals()`? That would be why. – ha9u63a7 Nov 10 '14 at 20:12
  • We have to see your code for `TrueFalseQuestion` – dkatzel Nov 10 '14 at 20:22
  • No comparisons happen until this point in my code, I'm just directly printing what I have inserted in the previous step. I generally use .equals(), yes. – Deena Osman Nov 10 '14 at 20:23
  • Um, you still haven't posted the `TrueFalseQuestion` code. – RealSkeptic Nov 10 '14 at 20:32
  • public class TrueFalseQuestion extends Question { private boolean CorrectB; public TrueFalseQuestion(String qu, boolean b){ super(qu); } Override public void GetQuestion() { System.out.println(getMyText()+"\n Is this statement true or false?"); } Override public String GetAnswer() { System.out.println("Insert Answer: "); boolean MyAnswer=s.nextBoolean(); return Boolean.toString(MyAnswer); } – Deena Osman Nov 10 '14 at 20:45
  • Override public String CheckAnswer() { return Boolean.toString(GetAnswer().equalsIgnoreCase(Boolean.toString(GetCorrectAnswer()))); } /** * return the MyAnswer */ /** * return the CorrectAnswer */ public boolean GetCorrectAnswer() { return CorrectB; } /** * return the MyAnswer */ } – Deena Osman Nov 10 '14 at 20:45
  • Excuse my slow responses.@RealSkeptic – Deena Osman Nov 10 '14 at 20:46
  • @dkatzel Check this out. Really sorry for the confusion. – Deena Osman Nov 10 '14 at 20:47

2 Answers2

0

Problem is nextInt() leaves "\n" in the stream and when next() executes in String Q2=s.next(); It reads "\n" and doesn't gives you an opportunity to input Question.

Try this:

System.out.println("How many true or false questions would you like to include in your quiz?");
int l=Integer.parseInt(nextLine());
TrueFalseQuestion[] qu2= new TrueFalseQuestion[l];
int x;
for(x=0;x<l;x++){
    System.out.println("Please insert question "+(x+1)+":\n");
    String Q2=s.nextLine();
    System.out.println("Please insert the correct answer");
    boolean A2=s.nextBoolean();
    qu2[x]=new TrueFalseQuestion(Q2,A2);
    System.out.println(qu2[x].GetCorrectAnswer());
}
AsSiDe
  • 1,826
  • 2
  • 15
  • 24
0

your problem is your constructor:

public TrueFalseQuestion(String qu, boolean b){ 
    super(qu);
}

You don't set the field CorrectB so it defaults to its default value which in java is false

the fix is

public TrueFalseQuestion(String qu, boolean b){ 
    super(qu);
    CorrectB = b;
}
dkatzel
  • 31,188
  • 3
  • 63
  • 67