package org.kodejava.example.util;

import java.util.Scanner;

public class ScannerValidateInput {
    public static void main(String[] args) {
        ScannerValidateInput demo = new ScannerValidateInput();
        demo.validatePositiveNumber();
    }

    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 < 0);

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

@@ -236,24 +236,55 @@
 veNumber();%0A
+        demo.validateLetter();%0A
     %7D%0A%0A    p
@@ -809,11 +809,694 @@
 );%0A    %7D
+%0A%0A    private void validateLetter() %7B%0A        String guestWord = %22Hello%22;%0A        Scanner scanner = new Scanner(System.in);%0A%0A        int length = 0;%0A        StringBuilder guess = new StringBuilder();%0A        do %7B%0A            System.out.print(%22Enter a letter to guess: %22);%0A            char letter = scanner.next().charAt(0);%0A            if (Character.isLetter(letter)) %7B%0A                guess.append(letter);%0A                length++;%0A            %7D%0A        %7D while (length %3C guestWord.length());%0A%0A        if (guestWord.equals(guess.toString())) %7B%0A            System.out.println(%22You are correct!%22);%0A        %7D else %7B%0A            System.out.println(%22Please try again!%22);%0A        %7D%0A    %7D
 %0A%7D%0A

Update examples.