I know that this sounds like a repeat question, but I did spend all day yesterday extensively searching on both Stack and Google and while there were different solutions out there (one in which I'm basing my own attempt on) I feel I've reached a point where I need some guidance with my code.
I want to simply create a scanner that only accepts values 1 through 50. In my main method, I have the following:
import java.util.Scanner;
public class test {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int inputInt = getInput(in);
System.out.print(inputInt);
}
public static int getInput(Scanner in) {
System.out.println("Enter the number of questions (between 1-50):");
int input = 0;
while (true) {
input = in.nextInt();
if(input > 50 || input < 1)
break;
System.out.print("Invalid input.\nEnter the number of questions (between 1-50):");
}
return input;
}
}
It does not seem to give me the "Invalid input" error whenever the input is higher than 50 or below 1. I've been trying to search for the past 2 days for a solution and every solution I've found had its own problems, and trying to solve each just dug me deeper and deeper into a hole. Questions such as [1] [2] [3] [4] [5] [6]. I feel like at this point I can't figure this out without a little guidance.