Question:
Design an algorithm that prompts the user to enter a number that is greater than 99 and validates the input.
Code:
import java.util.*; public class ReadInt { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int i=0; try { System.out.print("Enter Integer: "); i = sc.nextInt(); System.out.println("You entered: " + i); if(i > 99){ System.out.println(i + " is greater than 99"); } else { System.out.println(i + " is NOT greater than 99"); } } catch (InputMismatchException e) { System.out.println("You did not enter a number."); } } }
Output:
$ java ReadInt Enter Integer: 55 You entered: 55 55 is NOT greater than 99 $ java ReadInt Enter Integer: 100 You entered: 100 100 is greater than 99 $ java ReadInt Enter Integer: foo You did not enter a number.