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

Untitled

By: a guest on Jul 6th, 2012  |  syntax: None  |  size: 7.72 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. // A version of ConsoleReader() specialised to the standard input
  3. // for convenience of reading text from the keyboard.
  4. // For example,
  5. //
  6. //      j = Console.readInt(); b = Console.readBoolean();
  7. //
  8. // behaves identically to
  9. //
  10. //      ConsoleReader console = new ConsoleReader();
  11. //      j = console.readInt(); b = console.readBoolean();
  12. //
  13. // Author: J. M. Morris (jmorris@compapp.dcu.ie).
  14. // Version 1.5      Februaey 11th 2002.
  15. //
  16. // Example of use: Execution of the statements
  17. //  
  18. // j = Console.readInt(); b = Console.readBoolean(); c = Console.readChar();
  19. // t = Console.readToken(); s = Console.readString();
  20. //
  21. // with input (where - represents a space, and ended with RETURN)
  22. //
  23. // ------23----TRue-X---Java123---the--rest---
  24. //
  25. // would leave j=23,  b=true,  c='X',  t="Java123", and
  26. // s= "--the--rest---"
  27. //
  28.  
  29. import java.io.*;
  30.  
  31. public class Console {
  32.  
  33.     private static BufferedReader br = new BufferedReader(
  34.                 new InputStreamReader(System.in)); // the input stream
  35.     private static String buffer = "";
  36.     private static int p = 1; // buffer[p..] contains next input
  37.  
  38.     private static String getToken() throws IOException { // return next token from input stream
  39.         while (buffer != null && (p>= buffer.length() ||
  40.                 Character.isWhitespace(buffer.charAt(p)))) {
  41.             if (p>= buffer.length()) {
  42.                 buffer = br.readLine();
  43.                 p = 0;
  44.             }
  45.             else p++;
  46.         }
  47.         if (buffer == null) throw new IOException("Console: Unexpected end of file");
  48.         int t = p;
  49.         p++;
  50.         while(p < buffer.length() &&
  51.                 !(Character.isWhitespace(buffer.charAt(p))))
  52.             p++;
  53.         p++;
  54.         return(buffer.substring(t,p-1));
  55.     }
  56.  
  57.     public static int readInt() {
  58.     // Consume and return an integer. Trailing delimiter consumed.
  59.         try {  
  60.             return Integer.parseInt(getToken());
  61.         }
  62.         catch (Exception e) {
  63.             System.err.println("Console: IO Exception in readInt");
  64.             return 0;
  65.         }
  66.     }  
  67.  
  68.     public static boolean readBoolean() {
  69.     // Consume and return a boolean. Trailing delimiter consumed.
  70.     // Any string other than "true" (case ignored) is treated as false.
  71.         try {  
  72.             return new Boolean(getToken()).booleanValue();
  73.         }
  74.         catch (Exception e) {
  75.             System.err.println("Console: IO Exception in readBoolean");
  76.             return false;
  77.         }
  78.     }
  79.  
  80.     public static double readDouble() {
  81.     // Consume and return a double. Trailing delimiter consumed.
  82.         try {
  83.             return new Double(getToken()).doubleValue();
  84.         }
  85.         catch (Exception ioe) {
  86.             System.err.println("Console: IO Exception in readDouble");
  87.             return 0.0;
  88.         }
  89.     }
  90.  
  91.     public static String readToken() {
  92.     // Consume and return a token. Trailing delimiter consumed.
  93.     // A token is a maximal sequence of non-whitespace characters.
  94.     // null returned on end of file
  95.         try {
  96.             while (buffer != null && (p>= buffer.length() ||
  97.                 Character.isWhitespace(buffer.charAt(p)))) {
  98.                 if (p>= buffer.length()) {
  99.                     buffer = br.readLine();
  100.                     p = 0;
  101.                 }
  102.                 else p++;
  103.             }
  104.             if (buffer == null) return null;
  105.             int t = p;
  106.             p++;
  107.             while(p < buffer.length() &&
  108.                     !(Character.isWhitespace(buffer.charAt(p))))
  109.                 p++;
  110.             p++;
  111.             return(buffer.substring(t,p-1));
  112.         }
  113.         catch (IOException ioe) {
  114.             System.err.println("Console: IO Exception in readToken");
  115.             return "";
  116.         }
  117.     }
  118.      
  119.     public static char readChar() {
  120.     //Consume and return a character (which may be an end-of-line).
  121.         try {
  122.             if (buffer != null && p>buffer.length()) {
  123.                 buffer = br.readLine();
  124.                 p = 0;
  125.             }
  126.             if (buffer == null) throw new IOException("Console: Unexpected end of file in readChar");
  127.             if (p == buffer.length()) { // supply end-of-line
  128.                 p++;
  129.                 return('\n');
  130.             }      
  131.             else {
  132.                 p++;
  133.                 return buffer.charAt(p-1);
  134.             }
  135.         }
  136.         catch (IOException ioe) {
  137.                 System.err.println("Console: IO Exception in readChar");
  138.                 return (char)0;
  139.         }
  140.     }
  141.  
  142.     public static char peekChar() {
  143.     // The next available character if any (which may be an end-of-line). The
  144.     // character is not consumed. If buffer is empty return null character.
  145.         if (buffer == null || p>buffer.length()) return('\000');
  146.         else if (p == buffer.length()) return('\n');
  147.         else return buffer.charAt(p);
  148.     }
  149.  
  150.     public static String readString() {
  151.     // Consume and return the remainder of current line (end-of-line discarded).
  152.     // null returned on end of file
  153.         try {
  154.             if (buffer!= null && p>buffer.length()) {
  155.                 buffer = br.readLine();
  156.                 p = 0;
  157.             }  
  158.             if (buffer == null) return null;
  159.             int t = p;  p = buffer.length() + 1;
  160.             return buffer.substring(t);
  161.        }
  162.        catch (IOException ioe) {
  163.             System.err.println("Console: IO Exception in readString");
  164.             return "";
  165.        }
  166.     }
  167.  
  168.     public static int available() {
  169.     // Number of characters available on this line (including end-of-line,
  170.     // which counts as one character, i.e. '\n')
  171.         if (buffer == null) return 0;
  172.         else return (buffer.length()+1-p);
  173.     }
  174.      
  175.     public static boolean hasMoreTokens() {
  176.     // Are there more tokens on the current line?
  177.         if (buffer == null) return false;
  178.         int q = p;
  179.         while (q<buffer.length() && Character.isWhitespace(buffer.charAt(q))) q++;
  180.         return (q<buffer.length());
  181.     }
  182.      
  183.     public static void skipLine() {
  184.     // Skip any remaining input on this line.
  185.         if (buffer != null) p = buffer.length() + 1;
  186.     }
  187.  
  188.     public static void skipWhitespace() {
  189.     // Consumes input until a non-whitespace character is entered (which
  190.     // is not consumed).
  191.         try {
  192.             while (buffer != null && (p>= buffer.length() ||
  193.                     Character.isWhitespace(buffer.charAt(p)))) {
  194.                 if (p>= buffer.length()) {
  195.                         buffer = br.readLine();
  196.                     p = 0;
  197.                 }
  198.                 else p++;
  199.             }
  200.         }
  201.         catch (IOException ioe) {
  202.             System.err.println("Console: IO Exception in skipWhitespace");
  203.         }
  204.     }  
  205.      
  206.     public static boolean EndOfFile() { // More characters?
  207.         // This method is intended for use when keyboard is redirected to file
  208.         if (available()>0) return false;
  209.         try {
  210.             buffer = br.readLine();
  211.         }
  212.         catch (IOException ioe) {
  213.             System.err.println("Console: IO Exception in EndOfFile");
  214.         }
  215.         p = 0;
  216.         return (buffer == null);
  217.     }        
  218.  
  219.     public static boolean endOfFile() { // More characters?
  220.     // alternative spelling for EndOfFile()
  221.         // This method is intended for use when keyboard is redirected to file
  222.         if (available()>0) return false;
  223.         try {
  224.             buffer = br.readLine();
  225.         }
  226.         catch (IOException ioe) {
  227.             System.err.println("Console: IO Exception in EndOfFile");
  228.         }
  229.         p = 0;
  230.         return (buffer == null);
  231.     }        
  232.  
  233. }