import java.io.*; /** * WhileEx1 --- This program reads user input from the keyboard * it checks if it is a five. It will loop either until the enter * types a five or after three failed attempts * * @author Blanca Polo **/ public class WhileEx1{ /** * Reads a number from the user, checks if it is indeed a number. * it iterates until the user enters a five or after * three failed attempts to do so. * @param arg A string array containing the command line arguments. * @return No return value. */ public static void main (String arg[ ]) throws Exception{ final int NUMBER = 5; // constant final int ATTEMPTS = 3; InputStreamReader isr = new InputStreamReader(System.in); BufferedReader br = new BufferedReader(isr); String sNumber = ""; int iNumber= 0; boolean done = false; // control variable; int iCount = 0; // to count the failed attempts //while works as long as the condition is true while(!done){ //translated as not(false) which is true. System.out.print("Type number five please: "); try{ sNumber = br.readLine( ); sNumber = sNumber.trim( ); iNumber = Integer.parseInt(sNumber); if (iNumber != NUMBER){ System.out.println("You did not type five, try again!!!"); done = false; } else{ System.out.println("Thanks for the number five, you are so smart!!" ); done = true; } } //ends try catch(NumberFormatException nfe){ System.out.println( "this is your attempt number "+ iCount); System.out.println( "you did not type a number, try again!!!"); done = false; } iCount++; System.out.println("\t\tAttempts = " + iCount); if(iCount >= ATTEMPTS){ done = true; } } //closes while System.out.println("I'm out of here" ); } //main } //class