calcpage

StdIn.java

Jun 12th, 2012
125
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5 5.93 KB | None | 0 0
  1. /*************************************************************************
  2.  *  Compilation:  javac StdIn.java
  3.  *  Execution:    java StdIn
  4.  *
  5.  *  Reads in data of various types from standard input.
  6.  *
  7.  *************************************************************************/
  8.  
  9. import java.io.BufferedInputStream;
  10. import java.util.Locale;
  11. import java.util.Scanner;
  12.  
  13. /**
  14.  *  <i>Standard input</i>. This class provides methods for reading strings
  15.  *  and numbers from standard input.
  16.  *  <p>
  17.  *  The Locale used is: language = English, country = US. This is consistent
  18.  *  with the formatting conventions with Java floating-point literals,
  19.  *  command-line arguments (via <tt>Double.parseDouble()</tt>)
  20.  *  and standard output (via <tt>System.out.print()</tt>). It ensures that
  21.  *  standard input works with the input files used in the textbook.
  22.  *  <p>
  23.  *  For additional documentation, see <a href="http://introcs.cs.princeton.edu/15inout">Section 1.5</a> of
  24.  *  <i>Introduction to Programming in Java: An Interdisciplinary Approach</i> by Robert Sedgewick and Kevin Wayne.
  25.  */
  26. public final class StdIn {
  27.  
  28.     // assume Unicode UTF-8 encoding
  29.     private static String charsetName = "UTF-8";
  30.  
  31.     // assume language = English, country = US for consistency with System.out.
  32.     private static Locale usLocale = new Locale("en", "US");
  33.  
  34.     // the scanner object
  35.     private static Scanner scanner = new Scanner(new BufferedInputStream(System.in), charsetName);
  36.  
  37.     // static initializer
  38.     static { scanner.useLocale(usLocale); }
  39.  
  40.     // singleton pattern - can't instantiate
  41.     private StdIn() { }
  42.  
  43.  
  44.     /**
  45.      * Is there only whitespace left on standard input?
  46.      */
  47.     public static boolean isEmpty() {
  48.         return !scanner.hasNext();
  49.     }
  50.  
  51.     /**
  52.      * Return next string from standard input
  53.      */
  54.     public static String readString() {
  55.         return scanner.next();
  56.     }
  57.  
  58.     /**
  59.      * Return next int from standard input
  60.      */
  61.     public static int readInt() {
  62.         return scanner.nextInt();
  63.     }
  64.  
  65.     /**
  66.      * Return next double from standard input
  67.      */
  68.     public static double readDouble() {
  69.         return scanner.nextDouble();
  70.     }
  71.  
  72.     /**
  73.      * Return next float from standard input
  74.      */
  75.     public static float readFloat() {
  76.         return scanner.nextFloat();
  77.     }
  78.  
  79.     /**
  80.      * Return next short from standard input
  81.      */
  82.     public static short readShort() {
  83.         return scanner.nextShort();
  84.     }
  85.  
  86.     /**
  87.      * Return next long from standard input
  88.      */
  89.     public static long readLong() {
  90.         return scanner.nextLong();
  91.     }
  92.  
  93.     /**
  94.      * Return next byte from standard input
  95.      */
  96.     public static byte readByte() {
  97.         return scanner.nextByte();
  98.     }
  99.  
  100.     /**
  101.      * Return next boolean from standard input, allowing "true" or "1" for true,
  102.      * and "false" or "0" for false
  103.      */
  104.     public static boolean readBoolean() {
  105.         String s = readString();
  106.         if (s.equalsIgnoreCase("true"))  return true;
  107.         if (s.equalsIgnoreCase("false")) return false;
  108.         if (s.equals("1"))               return true;
  109.         if (s.equals("0"))               return false;
  110.         throw new java.util.InputMismatchException();
  111.     }
  112.  
  113.     /**
  114.      * Does standard input have a next line?
  115.      */
  116.     public static boolean hasNextLine() {
  117.         return scanner.hasNextLine();
  118.     }
  119.  
  120.     /**
  121.      * Return rest of line from standard input
  122.      */
  123.     public static String readLine() {
  124.         return scanner.nextLine();
  125.     }
  126.  
  127.     /**
  128.      * Return next char from standard input
  129.      */
  130.     // a complete hack and inefficient - email me if you have a better
  131.     public static char readChar() {
  132.         // (?s) for DOTALL mode so . matches a line termination character
  133.         // 1 says look only one character ahead
  134.         // consider precompiling the pattern
  135.         String s = scanner.findWithinHorizon("(?s).", 1);
  136.         return s.charAt(0);
  137.     }
  138.  
  139.     /**
  140.      * Return rest of input from standard input
  141.      */
  142.     public static String readAll() {
  143.         if (!scanner.hasNextLine()) return null;
  144.  
  145.         // reference: http://weblogs.java.net/blog/pat/archive/2004/10/stupid_scanner_1.html
  146.         return scanner.useDelimiter("\\A").next();
  147.     }
  148.  
  149.    /**
  150.      * Read rest of input as array of ints
  151.      */
  152.     public static int[] readInts() {
  153.         String[] fields = readAll().trim().split("\\s+");
  154.         int[] vals = new int[fields.length];
  155.         for (int i = 0; i < fields.length; i++)
  156.             vals[i] = Integer.parseInt(fields[i]);
  157.         return vals;
  158.     }
  159.  
  160.    /**
  161.      * Read rest of input as array of doubles
  162.      */
  163.     public static double[] readDoubles() {
  164.         String[] fields = readAll().trim().split("\\s+");
  165.         double[] vals = new double[fields.length];
  166.         for (int i = 0; i < fields.length; i++)
  167.             vals[i] = Double.parseDouble(fields[i]);
  168.         return vals;
  169.     }
  170.  
  171.    /**
  172.      * Read rest of input as array of strings
  173.      */
  174.     public static String[] readStrings() {
  175.         String[] fields = readAll().trim().split("\\s+");
  176.         return fields;
  177.     }
  178.  
  179.  
  180.  
  181.     /**
  182.      * Unit test
  183.      */
  184.     public static void main(String[] args) {
  185.  
  186.         System.out.println("Type a string: ");
  187.         String s = StdIn.readString();
  188.         System.out.println("Your string was: " + s);
  189.         System.out.println();
  190.  
  191.         System.out.println("Type an int: ");
  192.         int a = StdIn.readInt();
  193.         System.out.println("Your int was: " + a);
  194.         System.out.println();
  195.  
  196.         System.out.println("Type a boolean: ");
  197.         boolean b = StdIn.readBoolean();
  198.         System.out.println("Your boolean was: " + b);
  199.         System.out.println();
  200.  
  201.         System.out.println("Type a double: ");
  202.         double c = StdIn.readDouble();
  203.         System.out.println("Your double was: " + c);
  204.         System.out.println();
  205.  
  206.     }
  207.  
  208. }
Add Comment
Please, Sign In to add comment