This code checks if the users input it is valid. if it is not a number it will continue to loop untill it receives a number. after that it will check if that number is in bounds or lesser than than the bound. it will continue looping until it receives an inbound number. but my problem here is that when I print choice it only shows the previous number after the last number that was inserted. why is it like that?
public void askForDifficulty(){
System.out.println("Difficulty For This Question:\n1)Easy\n2)Medium\n3)Hard\nChoice: ");
int choice = 0;
boolean notValid = true;
boolean notInbound = true;
do{
while(!input.hasNextInt()){
System.out.println("Numbers Only!");
System.out.print("Try again: ");
input.nextLine();
}
notValid = false;
choice = input.nextInt();
}while(notValid);
do{
while(input.nextInt() > diff.length){
System.out.println("Out of bounds");
input.nextLine();
}
choice = input.nextInt();
notInbound = false;
}while(notInbound);
System.out.println(choice);
}
This is because
input.nextInt()inside thewhilecondition consumes the integer, so the one after it reads the following one. EDIT You also need to combine the two loops, like this: