/** * KXT101 Prac Test 2 * * Discovering the secret letter * * PracTest2.java * * @author Matthew Bishop * @version 24 April 2010 * purpose: * allow the user to enter words and check to * see if those words contain a secret letter * */ import java.util.Scanner; public class PracTest2 { public static void main(String[] args) { // the secret letter to match user input against final char SECRET_LETTER = 'r'; // the word which the user enters String word = ""; // a scanner to capture the user input Scanner scan = new Scanner(System.in); // the loop which allows the user to enter a series // of words until he or she types 'stop' while (!word.equalsIgnoreCase("stop")) { if (word.equals("")) { System.out.println( "Hello, this program checks a word for a secret letter \n"); System.out.println("enter your word, stop to end."); } else { System.out.println("enter next word, stop to end."); } word = scan.next(); // only for debugging // System.out.println("your word is:" + word); scan.nextLine(); // tell the user if their word contains the secret // letter, or if the user has typed 'stop' dont print // any message. if (!word.equalsIgnoreCase("stop")) { if (word.indexOf(SECRET_LETTER) == -1) { System.out.println( "Your guess does not contain the secret letter"); } else { System.out.println( "Your guess contains the secret letter"); } } } scan.close(); } }