User prompts for entering quiz questions I have two methods here  that does the same thing: get the user input and validate them.

If they enter a letter, the program will prompt them again to enter a number. |After that, if it's a number it will compare it to the bound given by the program or the user. It is working fine, but can anybody help me refactor this code? It would be really helpful if I could dissect the validations into smaller pieces of code or methods.

public void addToQuestionToQbank(){
        this.askForCategory();
        System.out.println("Add Questions[1]Multiple Choice[2] Identification: ");
        int choice;
        boolean notValid = true;
        do{
            while(!input.hasNextInt()){
                System.out.println("Numbers Only!");
                System.out.print("Try again: ");
                input.nextLine();
            }
                notValid = false;
                choice = input.nextInt();
        }while(notValid);

        while(choice &gt; 2){
            System.out.println("Out of bounds");
            choice = input.nextInt();
            input.nextLine();
        }
    }


public Difficulty askForDifficulty(){
        System.out.println("Difficulty For This Question:\n1)Easy\n2)Medium\n3)Hard\nChoice: ");
        int choice = 0;

        boolean notValid = true;
        do{
            while(!input.hasNextInt()){
                System.out.println("Numbers Only!");
                System.out.print("Try again: ");
                input.nextLine();
            }
                notValid = false;
                choice = input.nextInt();
        }while(notValid);

        while(choice &gt; diff.length){
            System.out.println("Out of bounds");
            choice = input.nextInt();
            input.nextLine();
        }
        System.out.println(diff[choice -1]);
       return diff[choice -1];
    }
There are 2 problems that must be addressed first. Where did diff.choice come from, what is that? Second. What is the this.askForCategory() call? It sounds like exactly what addToQuestionToQbank() is doing. addToQuestionToQbank() does not do what it says, so I wonder what the real adding method looks like ....

Anyway, I would expect the calling code to look something like this:

public void newQuestion () {
    Question myQuestion = new Question();

    myQuestion.Question = this.askForQuestion();
    myQuestion.Answer = this.askForAnswer ();

    // here is where we call your code, refactored.
    myQuestion.category = this.multiChoicePrompt("Add Questions[1]Multiple Choice[2] Identification: ", 2);
    myQuestion.Difficulty = this.multiChoicePrompt("Difficulty For This Question:\n1)Easy\n2)Medium\n3)Hard\nChoice: " ,3);

    this.addQuestionToQbank(myQuestion);
}


By passing the prompt as a parameter we can now refactor as you asked:

// this prompt designed specifically to pick from multiple choices
// note how the method name makes that comment irrelevant.
public int multiChoicePrompt(String askUser, int responseMaxValue) {
    System.out.println(askUser);

    int choice;
    boolean notValid = true;
    do{
        while(!input.hasNextInt()){
            System.out.println("Numbers Only!");
            System.out.print("Try again: ");
            input.nextLine();
        }
            notValid = false;
            choice = input.nextInt();
    }while(notValid);

    // assume numbered choices always start with "1"
    while(choice &gt; responseMaxValue || choice &lt; 1){
        System.out.println("Out of bounds");
        choice = input.nextInt();
        input.nextLine();
    }
    return choice;
} 


The easiest way to "extract method" might be to physically move all that code to the new method and then recompile and let the compiler errors tell you what variables it doesn't "know"  - these are what you pass in as parameters.