Marcus Richard
Marcus Richard

Reputation: 7

Trouble setting a Parameter

Im confused as to how I allow only numbers 1-4? Im not sure if there is a term for this, i think its parameter

THE CODE IM QUESTIONING IS THE 3RD TO LAST LINE

private void validatePositiveNumber() {
    Scanner scanner = new Scanner(System.in);

    int number;
    do {
        System.out.print("Please enter a positive number: ");
        while (!scanner.hasNextInt()) {
            String input = scanner.next();
            System.out.printf("\"%s\" is not a valid number.\n", input);
        }
        number = scanner.nextInt();
    } while (number < 4);

    System.out.printf("You have entered a positive number %d.\n", number);
}

Upvotes: 0

Views: 41

Answers (2)

RAIMT TEAM
RAIMT TEAM

Reputation: 1

Scanner scanner = new Scanner(System.in);

        int number;
        do {
            System.out.print("Please enter a positive number: ");
            while (!scanner.hasNextInt()) {
                String input = scanner.next();
                System.out.printf("\"%s\" is not a valid number.\n", input);
            }
            number = scanner.nextInt();
        } while (number > 4 || number < 1);

        System.out.printf("You have entered a positive number %d.\n", number);

Upvotes: 0

vladzaba
vladzaba

Reputation: 1402

Use while (number > 4 || number < 1); This disallows anything outside of the range.

Upvotes: 1

Related Questions