1 23 24 package com.sun.enterprise.util; 25 26 33 34 public class Console 35 { 36 39 40 public static void printPrompt(String prompt) 41 { 42 System.out.print(prompt + " "); 43 System.out.flush(); 44 } 45 46 50 51 public static String readLine() 52 { 53 int ch; 54 String r = ""; 55 boolean done = false; 56 57 while (!done) 58 { 59 try 60 { 61 ch = System.in.read(); 62 if (ch < 0 || (char)ch == '\n') 63 done = true; 64 65 else if ((char)ch != '\r') r = r + (char) ch; 67 } 68 catch(java.io.IOException e) 69 { 70 done = true; 71 } 72 } 73 74 return r; 75 } 76 77 82 83 public static char getKey(String prompt) 84 { 85 printPrompt(prompt); 86 int ch = '\n'; 87 88 try 89 { 90 ch = System.in.read(); 91 } 92 catch(java.io.IOException e) 93 { 94 } 95 return (char)ch; 96 97 } 98 99 104 105 public static String readLine(String prompt) 106 { 107 printPrompt(prompt); 108 return readLine(); 109 } 110 111 117 118 public static int readInt(String prompt) 119 { 120 while(true) 121 { 122 printPrompt(prompt); 123 124 try 125 { 126 return Integer.valueOf 127 (readLine().trim()).intValue(); 128 } 129 catch(NumberFormatException e) 130 { 131 System.out.println("Not an integer. Please try again!"); 132 } 133 } 134 } 135 136 142 143 public static double readDouble(String prompt) 144 { 145 while(true) 146 { 147 printPrompt(prompt); 148 149 try 150 { 151 return Double.parseDouble(readLine().trim()); 152 } 153 catch(NumberFormatException e) 154 { 155 System.out.println("Not a floating point number. Please try again!"); 156 } 157 } 158 } 159 } 160 | Popular Tags |