scg/ch14/io/Keyboard

From FANG

Jump to: navigation, search

001 package scg.ch14.io;
002 
003 import java.util.Scanner;
004 
005 /**
006  * A utility class wrapped around standard input. Provides all static
007  * methods to prompt the user and read information from the user.
008  */
009 public class Keyboard {
010   /** scanner wrapped around standard input */
011   static Scanner keyboard = new Scanner(System.in);
012 
013   /**
014    * Loop until user enters either 'y', 'n', 'yes', or 'no'.
015    *
016    @return  true if user answered 'y' or 'yes'; false otherwise
017    */
018   public static boolean answeredYes() {
019     return answeredYesPrompt("");
020   }
021 
022   /**
023    * Print the prompt on the standard output until the user answers
024    * either 'y', 'n', 'yes', or 'no'.
025    *
026    @param   prompt  the prompt to show the user; nothing is printed if
027    *                  the prompt is blank
028    *
029    @return  true if user answered 'y' or 'yes'; false otherwise
030    */
031   public static boolean answeredYesPrompt(String prompt{
032     String userAnswer = "";
033     // sentinel: userAnswer is a valid answer
034     while (!userAnswer.equalsIgnoreCase("y"&&
035         !userAnswer.equalsIgnoreCase("n"&&
036         !userAnswer.equalsIgnoreCase("yes"&&
037         !userAnswer.equalsIgnoreCase("no")) {
038       showPrompt(prompt);
039       userAnswer = keyboard.nextLine();
040     }
041     // if we get here with "yes" or "y" we return true. Just check first
042     // character (ignore case)
043     return userAnswer.substring(01).equalsIgnoreCase("y");
044   }
045 
046   /**
047    * Get the next word from the keyboard. Will block, waiting for user,
048    if there is no input ready. DISPLAYS NO PROMPT!
049    *
050    @return  the next {@link String} entered by the user; contains no
051    *          whitespace and is not empty
052    */
053   public static String next() {
054     return nextPrompt("");
055   }
056 
057   /**
058    * Get the next line form the keyboard without displaying a prompt.
059    * DISPLAYS NO PROMPT!
060    *
061    @return  the next line entered by user
062    */
063   public static String nextLine() {
064     return nextLinePrompt("");
065   }
066 
067   /**
068    * Print the prompt on standard output, read the next line from the
069    * standard input and return that value.
070    *
071    @param   prompt  the prompt/question to show the user
072    *
073    @return  the {@link String} typed in by the user; it could be empty
074    */
075   public static String nextLinePrompt(String prompt{
076     showPrompt(prompt);
077     return keyboard.nextLine();
078   }
079 
080   /**
081    * Prompt user and read the next word from standard input, returning
082    * that word.
083    *
084    @param   prompt  the prompt/question to show the user; nothing is
085    *                  printed if the prompt is blank
086    *
087    @return  the {@link String} typed in by the user; contains no
088    *          whitespace and is not empty
089    */
090   public static String nextPrompt(String prompt{
091     showPrompt(prompt);
092     return keyboard.next();
093   }
094 
095   private static void showPrompt(String prompt{
096     if (prompt.length() != 0{
097       System.out.print(prompt + " ");
098     }
099   }
100 }
101 
102 //Uploaded on Mon Mar 29 21:39:23 EDT 2010


Download/View scg/ch14/io/Keyboard.java





Views
Personal tools
Add to 
del.icio.usAdd to 
diggAdd to 
FacebookAdd to 
favoritesAdd to 
GoogleAdd to 
MySpaceAdd to 
PrintAdd to 
SlashdotAdd to 
StumbleUponAdd to 
Twitter

Games
Games