Hello I have a abstract class:

import javax.swing.JOptionPane;

public abstract class Question {
    static int nQuestions = 0;
    static int nCorrect = 0;

    String question;
    String correctAnswer;

    abstract String ask();

    void check() {
        nQuestions++;
        String answer = ask();
        if (answer.equals(correctAnswer)) {
            JOptionPane.showMessageDialog(null,"Correct!");
            nCorrect++;
            } else {
                JOptionPane.showMessageDialog(null,"Incorrect. The correctanswer is "+correctAnswer+".");
                }
        }
    static void showResults(){
        JOptionPane.showMessageDialog(null, nCorrect+" correct out of " +nQuestions+ " questions");
        }

}

It has a subclass:

import javax.swing.JOptionPane;
public class TrueFalseQuestion extends Question {

    TrueFalseQuestion(String question, String answer){
        this.question = "TRUE or FALSE: " +question;
        correctAnswer = answer;

    }

    @Override
    String ask() {
        while (true) {
            String answer = JOptionPane.showInputDialog(question);
            answer = answer.toUpperCase();
            boolean valid = (answer.equals("f") || answer.equals("false") ||
                    answer.equals("F") || answer.equals("False") ||  answer.equals("False") || 
                    answer.equals("FALSE") || answer.equals("n") ||
                    answer.equals("no") || answer.equals("NO") || answer.equals("t") ||
                    answer.equals("true") || answer.equals("T") || answer.equals("True") ||
                    answer.equals("TRUE"));
            if (valid) return answer;
            JOptionPane.showMessageDialog(null,"Invalid answer please enter True or False");
            }
    }

}

How can I make possible for correctAnswer to accept only True or Flase asnwers.

Use JOptionPane.showOptionDialog with options for True and False.
Eg:

        String[] options = {"False", "True"};

        int answer = JOptionPane.showOptionDialog(null,"Java is great?", "Question", 
                JOptionPane.DEFAULT_OPTION, JOptionPane.QUESTION_MESSAGE, null,
                options, null);

        if (answer < 0 ) System.out.println("User cancelled/closed dialog");
        else System.out.println(options[answer]);