scg/ch11/SentinelControlledLoop2

From FANG

Jump to: navigation, search

01 package scg.ch11;
02 
03 import java.util.Scanner;
04 
05 public class SentinelControlledLoop2 {
06   /**
07    static means there is only one; keyboard can be used in multiple
08    * methods
09    */
10   private static Scanner keyboard = new Scanner(System.in);
11 
12   /**
13    * Get a line from the user. Prints the prompt on the console followed
14    * by a space. Then waits for user to enter a line and returns the
15    * full line of text to the calling method.
16    *
17    @param   prompt  the prompt to print for the user.
18    *
19    @return  the line entered by the user (everything up to but not
20    *          including the <returnkey)
21    */
22   public static String getLine(String prompt{
23     System.out.print(prompt);
24     System.out.print(" ");
25     return keyboard.nextLine();
26   }
27 
28   /**
29    * Main program. Uses getLine to prompt user and read lines in a
30    * sentinel controlled loop. User enters the sentinel value "done"
31    * when they want to quit. All other lines are converted to upper-case
32    * and echoed back.
33    *
34    @param  args  command-line arguments - ignored by this program
35    */
36   public static void main(String[] args{
37     String line = getLine("Line to capitalize ('done' to finish):");
38     while (!line.equalsIgnoreCase("done")) {
39       System.out.println(line.toUpperCase());
40       line = getLine("Line to capitalize ('done' to finish):");
41     }
42   }
43 }
44 
45 //Uploaded on Mon Mar 29 21:39:19 EDT 2010


Download/View scg/ch11/SentinelControlledLoop2.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