0

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.

WoeIs
  • 1,013
  • 1
  • 6
  • 21
  • 1
    You simply `break` all incorrect inputs. The error message appears when the `if`-condition does not fit (when input is between 2-49. Btw, you can easily debug these snippets. Anyway, heads up. – elp Dec 23 '18 at 15:14

4 Answers4

5

You got your boundaries wrong! Obviously you meant:

if(input >= 1 && input <= 50)
    break;

That's because an input is valid when being between 1 and 50. In that case you break out.

Seelenvirtuose
  • 19,157
  • 6
  • 34
  • 57
3

This line:

System.out.print("Invalid input.\nEnter the number of questions (between 1-50):");

Will not show, because you have break before it. In this case, if input is wrong, break will happen before line should be displayed.

Ukrainis
  • 478
  • 2
  • 13
2

it seems like your while logic is backwards, try this:

   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 if you got the RIGHT number.
                break;
            System.out.print("Invalid input.\nEnter the number of questions (between 1-50):");
        }
        return input;
    }
Roee Gavirel
  • 17,192
  • 12
  • 58
  • 82
2

System.out.print("Invalid input.\nEnter the number of questions (between 1-50):"); should be inside the if condition like below

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){
                    System.out.print("Invalid input.\nEnter the number of questions (between 1-50):");
                    break;
                }
            }
            return input;
        }
    }
Sandeepa
  • 2,522
  • 3
  • 14
  • 34