Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on Jul 6th, 2012  |  syntax: None  |  size: 8.75 KB  |  hits: 9  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. ?
  2. 1
  3. 2
  4. 3
  5. 4
  6. 5
  7. 6
  8. 7
  9. 8
  10. 9
  11. 10
  12. 11
  13. 12
  14. 13
  15. 14
  16. 15
  17. 16
  18. 17
  19. 18
  20. 19
  21. 20
  22. 21
  23. 22
  24. 23
  25. 24
  26. 25
  27. 26
  28. 27
  29. 28
  30. 29
  31. 30
  32. 31
  33. 32
  34. 33
  35. 34
  36. 35
  37. 36
  38. 37
  39. 38
  40. 39
  41. 40
  42. 41
  43. 42
  44. 43
  45. 44
  46. 45
  47. 46
  48. 47
  49. 48
  50. 49
  51. 50
  52. 51
  53. 52
  54. 53
  55. 54
  56. 55
  57. 56
  58. 57
  59. 58
  60. 59
  61. 60
  62. 61
  63. 62
  64. 63
  65. 64
  66. 65
  67. 66
  68. 67
  69. 68
  70. 69
  71. 70
  72. 71
  73. 72
  74. 73
  75. 74
  76. 75
  77. 76
  78. 77
  79. 78
  80. 79
  81. 80
  82. 81
  83. 82
  84. 83
  85. 84
  86. 85
  87. 86
  88. 87
  89. 88
  90. 89
  91. 90
  92. 91
  93. 92
  94. 93
  95. 94
  96. 95
  97. 96
  98. 97
  99. 98
  100. 99
  101. 100
  102. 101
  103. 102
  104. 103
  105. 104
  106. 105
  107. 106
  108. 107
  109. 108
  110. 109
  111. 110
  112. 111
  113. 112
  114. 113
  115. 114
  116. 115
  117. 116
  118. 117
  119. 118
  120. 119
  121. 120
  122. 121
  123. 122
  124. 123
  125. 124
  126. 125
  127. 126
  128. 127
  129. 128
  130. 129
  131. 130
  132. 131
  133. 132
  134. 133
  135. 134
  136. 135
  137. 136
  138. 137
  139. 138
  140. 139
  141. 140
  142. 141
  143. 142
  144. 143
  145. 144
  146. 145
  147. 146
  148. 147
  149. 148
  150. 149
  151. 150
  152. 151
  153. 152
  154. 153
  155. 154
  156. 155
  157. 156
  158. 157
  159. 158
  160. 159
  161. 160
  162. 161
  163. 162
  164. 163
  165. 164
  166. 165
  167. 166
  168. 167
  169. 168
  170. 169
  171. 170
  172. 171
  173. 172
  174. 173
  175. 174
  176. 175
  177. 176
  178. 177
  179. 178
  180. 179
  181. 180
  182. 181
  183. 182
  184. 183
  185. 184
  186. 185
  187. 186
  188. 187
  189. 188
  190. 189
  191. 190
  192. 191
  193. 192
  194. 193
  195. 194
  196. 195
  197. 196
  198. 197
  199. 198
  200. 199
  201. 200
  202. 201
  203. 202
  204. 203
  205. 204
  206. 205
  207. 206
  208. 207
  209. 208
  210. 209
  211. 210
  212. 211
  213. 212
  214. 213
  215. 214
  216. 215
  217. 216
  218. 217
  219. 218
  220. 219
  221. 220
  222. 221
  223. 222
  224. 223
  225. 224
  226. 225
  227. 226
  228. 227
  229. 228
  230. 229
  231. 230
  232. 231
  233. 232
  234. // A version of ConsoleReader() specialised to the standard input
  235. // for convenience of reading text from the keyboard.
  236. // For example,
  237. //
  238. //      j = Console.readInt(); b = Console.readBoolean();
  239. //
  240. // behaves identically to
  241. //
  242. //      ConsoleReader console = new ConsoleReader();
  243. //      j = console.readInt(); b = console.readBoolean();
  244. //
  245. // Author: J. M. Morris (jmorris@compapp.dcu.ie).
  246. // Version 1.5      Februaey 11th 2002.
  247. //
  248. // Example of use: Execution of the statements
  249. //  
  250. // j = Console.readInt(); b = Console.readBoolean(); c = Console.readChar();
  251. // t = Console.readToken(); s = Console.readString();
  252. //
  253. // with input (where - represents a space, and ended with RETURN)
  254. //
  255. // ------23----TRue-X---Java123---the--rest---
  256. //
  257. // would leave j=23,  b=true,  c='X',  t="Java123", and
  258. // s= "--the--rest---"
  259. //
  260.  
  261. import java.io.*;
  262.  
  263. public class Console {
  264.  
  265.     private static BufferedReader br = new BufferedReader(
  266.                 new InputStreamReader(System.in)); // the input stream
  267.     private static String buffer = "";
  268.     private static int p = 1; // buffer[p..] contains next input
  269.  
  270.     private static String getToken() throws IOException { // return next token from input stream
  271.         while (buffer != null && (p>= buffer.length() ||
  272.                 Character.isWhitespace(buffer.charAt(p)))) {
  273.             if (p>= buffer.length()) {
  274.                 buffer = br.readLine();
  275.                 p = 0;
  276.             }
  277.             else p++;
  278.         }
  279.         if (buffer == null) throw new IOException("Console: Unexpected end of file");
  280.         int t = p;
  281.         p++;
  282.         while(p < buffer.length() &&
  283.                 !(Character.isWhitespace(buffer.charAt(p))))
  284.             p++;
  285.         p++;
  286.         return(buffer.substring(t,p-1));
  287.     }
  288.  
  289.     public static int readInt() {
  290.     // Consume and return an integer. Trailing delimiter consumed.
  291.         try {  
  292.             return Integer.parseInt(getToken());
  293.         }
  294.         catch (Exception e) {
  295.             System.err.println("Console: IO Exception in readInt");
  296.             return 0;
  297.         }
  298.     }  
  299.  
  300.     public static boolean readBoolean() {
  301.     // Consume and return a boolean. Trailing delimiter consumed.
  302.     // Any string other than "true" (case ignored) is treated as false.
  303.         try {  
  304.             return new Boolean(getToken()).booleanValue();
  305.         }
  306.         catch (Exception e) {
  307.             System.err.println("Console: IO Exception in readBoolean");
  308.             return false;
  309.         }
  310.     }
  311.  
  312.     public static double readDouble() {
  313.     // Consume and return a double. Trailing delimiter consumed.
  314.         try {
  315.             return new Double(getToken()).doubleValue();
  316.         }
  317.         catch (Exception ioe) {
  318.             System.err.println("Console: IO Exception in readDouble");
  319.             return 0.0;
  320.         }
  321.     }
  322.  
  323.     public static String readToken() {
  324.     // Consume and return a token. Trailing delimiter consumed.
  325.     // A token is a maximal sequence of non-whitespace characters.
  326.     // null returned on end of file
  327.         try {
  328.             while (buffer != null && (p>= buffer.length() ||
  329.                 Character.isWhitespace(buffer.charAt(p)))) {
  330.                 if (p>= buffer.length()) {
  331.                     buffer = br.readLine();
  332.                     p = 0;
  333.                 }
  334.                 else p++;
  335.             }
  336.             if (buffer == null) return null;
  337.             int t = p;
  338.             p++;
  339.             while(p < buffer.length() &&
  340.                     !(Character.isWhitespace(buffer.charAt(p))))
  341.                 p++;
  342.             p++;
  343.             return(buffer.substring(t,p-1));
  344.         }
  345.         catch (IOException ioe) {
  346.             System.err.println("Console: IO Exception in readToken");
  347.             return "";
  348.         }
  349.     }
  350.      
  351.     public static char readChar() {
  352.     //Consume and return a character (which may be an end-of-line).
  353.         try {
  354.             if (buffer != null && p>buffer.length()) {
  355.                 buffer = br.readLine();
  356.                 p = 0;
  357.             }
  358.             if (buffer == null) throw new IOException("Console: Unexpected end of file in readChar");
  359.             if (p == buffer.length()) { // supply end-of-line
  360.                 p++;
  361.                 return('\n');
  362.             }      
  363.             else {
  364.                 p++;
  365.                 return buffer.charAt(p-1);
  366.             }
  367.         }
  368.         catch (IOException ioe) {
  369.                 System.err.println("Console: IO Exception in readChar");
  370.                 return (char)0;
  371.         }
  372.     }
  373.  
  374.     public static char peekChar() {
  375.     // The next available character if any (which may be an end-of-line). The
  376.     // character is not consumed. If buffer is empty return null character.
  377.         if (buffer == null || p>buffer.length()) return('\000');
  378.         else if (p == buffer.length()) return('\n');
  379.         else return buffer.charAt(p);
  380.     }
  381.  
  382.     public static String readString() {
  383.     // Consume and return the remainder of current line (end-of-line discarded).
  384.     // null returned on end of file
  385.         try {
  386.             if (buffer!= null && p>buffer.length()) {
  387.                 buffer = br.readLine();
  388.                 p = 0;
  389.             }  
  390.             if (buffer == null) return null;
  391.             int t = p;  p = buffer.length() + 1;
  392.             return buffer.substring(t);
  393.        }
  394.        catch (IOException ioe) {
  395.             System.err.println("Console: IO Exception in readString");
  396.             return "";
  397.        }
  398.     }
  399.  
  400.     public static int available() {
  401.     // Number of characters available on this line (including end-of-line,
  402.     // which counts as one character, i.e. '\n')
  403.         if (buffer == null) return 0;
  404.         else return (buffer.length()+1-p);
  405.     }
  406.      
  407.     public static boolean hasMoreTokens() {
  408.     // Are there more tokens on the current line?
  409.         if (buffer == null) return false;
  410.         int q = p;
  411.         while (q<buffer.length() && Character.isWhitespace(buffer.charAt(q))) q++;
  412.         return (q<buffer.length());
  413.     }
  414.      
  415.     public static void skipLine() {
  416.     // Skip any remaining input on this line.
  417.         if (buffer != null) p = buffer.length() + 1;
  418.     }
  419.  
  420.     public static void skipWhitespace() {
  421.     // Consumes input until a non-whitespace character is entered (which
  422.     // is not consumed).
  423.         try {
  424.             while (buffer != null && (p>= buffer.length() ||
  425.                     Character.isWhitespace(buffer.charAt(p)))) {
  426.                 if (p>= buffer.length()) {
  427.                         buffer = br.readLine();
  428.                     p = 0;
  429.                 }
  430.                 else p++;
  431.             }
  432.         }
  433.         catch (IOException ioe) {
  434.             System.err.println("Console: IO Exception in skipWhitespace");
  435.         }
  436.     }  
  437.      
  438.     public static boolean EndOfFile() { // More characters?
  439.         // This method is intended for use when keyboard is redirected to file
  440.         if (available()>0) return false;
  441.         try {
  442.             buffer = br.readLine();
  443.         }
  444.         catch (IOException ioe) {
  445.             System.err.println("Console: IO Exception in EndOfFile");
  446.         }
  447.         p = 0;
  448.         return (buffer == null);
  449.     }        
  450.  
  451.     public static boolean endOfFile() { // More characters?
  452.     // alternative spelling for EndOfFile()
  453.         // This method is intended for use when keyboard is redirected to file
  454.         if (available()>0) return false;
  455.         try {
  456.             buffer = br.readLine();
  457.         }
  458.         catch (IOException ioe) {
  459.             System.err.println("Console: IO Exception in EndOfFile");
  460.         }
  461.         p = 0;
  462.         return (buffer == null);
  463.     }        
  464.  
  465. }