scg/ch10/SentinelControlledLoop

From FANG

Jump to: navigation, search

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


Download/View scg/ch10/SentinelControlledLoop.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