| 
001002
 003
 004
 005
 006
 007
 008
 009
 010
 011
 012
 013
 014
 015
 016
 017
 018
 019
 020
 021
 022
 023
 024
 025
 026
 027
 028
 029
 030
 031
 032
 033
 034
 035
 036
 037
 038
 039
 040
 041
 042
 043
 044
 045
 046
 047
 048
 049
 050
 051
 052
 053
 054
 055
 056
 057
 058
 059
 060
 061
 062
 063
 064
 065
 066
 067
 068
 069
 070
 071
 072
 073
 074
 075
 076
 077
 078
 079
 080
 081
 082
 083
 084
 085
 086
 087
 088
 089
 090
 091
 092
 093
 094
 095
 096
 097
 098
 099
 100
 101
 102
 103
 104
 105
 106
 107
 108
 109
 110
 111
 112
 113
 114
 115
 116
 117
 118
 119
 120
 121
 122
 123
 124
 125
 126
 127
 128
 129
 130
 131
 132
 133
 134
 135
 136
 137
 138
 139
 140
 141
 142
 143
 144
 145
 146
 147
 148
 149
 150
 151
 152
 153
 154
 155
 156
 157
 158
 159
 160
 161
 162
 163
 164
 165
 166
 167
 168
 169
 170
 171
 172
 173
 174
 175
 176
 177
 178
 179
 180
 181
 182
 183
 184
 185
 186
 187
 188
 189
 190
 191
 192
 193
 194
 195
 196
 197
 198
 199
 200
 201
 202
 203
 204
 205
 206
 207
 208
 209
 210
 211
 212
 213
 214
 215
 216
 217
 218
 219
 220
 221
 222
 223
 224
 225
 226
 227
 228
 229
 230
 231
 232
 233
 234
 235
 236
 237
 238
 239
 240
 241
 242
 243
 244
 245
 246
 247
 248
 249
 250
 251
 252
 253
 254
 255
 256
 257
 258
 259
 260
 261
 262
 263
 264
 265
 266
 267
 268
 269
 270
 271
 272
 273
 274
 275
 276
 277
 278
 279
 280
 281
 282
 283
 284
 285
 286
 287
 288
 289
 290
 291
 292
 293
 294
 295
 296
 297
 298
 299
 300
 301
 302
 303
 304
 305
 306
 307
 308
 309
 310
 311
 312
 313
 314
 315
 316
 317
 318
 319
 320
 321
 322
 
 | package stdlib;
/* ***********************************************************************
 *  Compilation:  javac StdIn.java
 *  Execution:    java StdIn   (interactive test of basic functionality)
 *
 *  Reads in data of various types from standard input.
 *
 *************************************************************************/
import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.util.Scanner;
import java.util.regex.Pattern;
/**
 *  <i>Standard input</i>. This class provides methods for reading strings
 *  and numbers from standard input. See
 *  <a href="http://introcs.cs.princeton.edu/15inout">Section 1.5</a> of
 *  <i>Introduction to Programming in Java: An Interdisciplinary Approach</i>
 *  by Robert Sedgewick and Kevin Wayne.
 *  <p>
 *  See the technical information in the documentation of the {@link In}
 *  class, which applies to this class as well.
 */
public final class StdIn {
  // it doesn't make sense to instantiate this class
  private StdIn() {}
  private static Scanner scanner;
  /* * begin: section (1 of 2) of code duplicated from In to StdIn */
  // assume Unicode UTF-8 encoding
  private static final String charsetName = "UTF-8";
  // assume language = English, country = US for consistency with System.out.
  private static final java.util.Locale usLocale =
      new java.util.Locale("en", "US");
  // the default token separator; we maintain the invariant that this value
  // is held by the scanner's delimiter between calls
  private static final Pattern WHITESPACE_PATTERN
  = Pattern.compile("\\p{javaWhitespace}+");
  // makes whitespace characters significant
  private static final Pattern EMPTY_PATTERN
  = Pattern.compile("");
  // used to read the entire input. source:
  // http://weblogs.java.net/blog/pat/archive/2004/10/stupid_scanner_1.html
  private static final Pattern EVERYTHING_PATTERN
  = Pattern.compile("\\A");
  /* * end: section (1 of 2) of code duplicated from In to StdIn */
  /* * begin: section (2 of 2) of code duplicated from In to StdIn,
   *  with all methods changed from "public" to "public static" ***/
  /**
   * Is the input empty (except possibly for whitespace)? Use this
   * to know whether the next call to {@link #readString()},
   * {@link #readDouble()}, etc will succeed.
   */
  public static boolean isEmpty() {
    return !scanner.hasNext();
  }
  /**
   * Does the input have a next line? Use this to know whether the
   * next call to {@link #readLine()} will succeed. <p> Functionally
   * equivalent to {@link #hasNextChar()}.
   */
  public static boolean hasNextLine() {
    return scanner.hasNextLine();
  }
  /**
   * Is the input empty (including whitespace)? Use this to know
   * whether the next call to {@link #readChar()} will succeed. <p> Functionally
   * equivalent to {@link #hasNextLine()}.
   */
  public static boolean hasNextChar() {
    scanner.useDelimiter(EMPTY_PATTERN);
    boolean result = scanner.hasNext();
    scanner.useDelimiter(WHITESPACE_PATTERN);
    return result;
  }
  /**
   * Read and return the next line.
   */
  public static String readLine() {
    String line;
    try                 { line = scanner.nextLine(); }
    catch (Exception e) { line = null;               }
    return line;
  }
  /**
   * Read and return the next character.
   */
  public static char readChar() {
    scanner.useDelimiter(EMPTY_PATTERN);
    String ch = scanner.next();
    assert (ch.length() == 1) : "Internal (Std)In.readChar() error!"
    + " Please contact the authors.";
    scanner.useDelimiter(WHITESPACE_PATTERN);
    return ch.charAt(0);
  }
  /**
   * Read and return the remainder of the input as a string.
   */
  public static String readAll() {
    if (!scanner.hasNextLine())
      return "";
    String result = scanner.useDelimiter(EVERYTHING_PATTERN).next();
    // not that important to reset delimeter, since now scanner is empty
    scanner.useDelimiter(WHITESPACE_PATTERN); // but let's do it anyway
    return result;
  }
  /**
   * Read and return the next string.
   */
  public static String readString() {
    return scanner.next();
  }
  /**
   * Read and return the next int.
   */
  public static int readInt() {
    return scanner.nextInt();
  }
  /**
   * Read and return the next double.
   */
  public static double readDouble() {
    return scanner.nextDouble();
  }
  /**
   * Read and return the next float.
   */
  public static float readFloat() {
    return scanner.nextFloat();
  }
  /**
   * Read and return the next long.
   */
  public static long readLong() {
    return scanner.nextLong();
  }
  /**
   * Read and return the next short.
   */
  public static short readShort() {
    return scanner.nextShort();
  }
  /**
   * Read and return the next byte.
   */
  public static byte readByte() {
    return scanner.nextByte();
  }
  /**
   * Read and return the next boolean, allowing case-insensitive
   * "true" or "1" for true, and "false" or "0" for false.
   */
  public static boolean readBoolean() {
    String s = readString();
    if (s.equalsIgnoreCase("true"))  return true;
    if (s.equalsIgnoreCase("false")) return false;
    if (s.equals("1"))               return true;
    if (s.equals("0"))               return false;
    throw new java.util.InputMismatchException();
  }
  /**
   * Read all strings until the end of input is reached, and return them.
   */
  public static String[] readAllStrings() {
    // we could use readAll.trim().split(), but that's not consistent
    // since trim() uses characters 0x00..0x20 as whitespace
    String[] tokens = WHITESPACE_PATTERN.split(readAll());
    if (tokens.length == 0 || tokens[0].length() > 0)
      return tokens;
    String[] decapitokens = new String[tokens.length-1];
    for (int i=0; i < tokens.length-1; i++)
      decapitokens[i] = tokens[i+1];
    return decapitokens;
  }
  /**
   * Read all ints until the end of input is reached, and return them.
   */
  public static int[] readAllInts() {
    String[] fields = readAllStrings();
    int[] vals = new int[fields.length];
    for (int i = 0; i < fields.length; i++)
      vals[i] = Integer.parseInt(fields[i]);
    return vals;
  }
  /**
   * Read all doubles until the end of input is reached, and return them.
   */
  public static double[] readAllDoubles() {
    String[] fields = readAllStrings();
    double[] vals = new double[fields.length];
    for (int i = 0; i < fields.length; i++)
      vals[i] = Double.parseDouble(fields[i]);
    return vals;
  }
  /* * end: section (2 of 2) of code duplicated from In to StdIn */
  /**
   * If StdIn changes, use this to reinitialize the scanner.
   */
  private static void resync() {
    setScanner(new Scanner(new java.io.BufferedInputStream(System.in),
        charsetName));
  }
  private static void setScanner(Scanner scanner) {
    StdIn.scanner = scanner;
    StdIn.scanner.useLocale(usLocale);
  }
  // do this once when StdIn is initialized
  static {
    resync();
  }
  /**
   * Reads all ints from stdin.
   * @deprecated For more consistency, use {@link #readAllInts()}
   */
  @Deprecated public static int[] readInts() {
    return readAllInts();
  }
  /**
   * Reads all doubles from stdin.
   * @deprecated For more consistency, use {@link #readAllDoubles()}
   */
  @Deprecated public static double[] readDoubles() {
    return readAllDoubles();
  }
  /**
   * Reads all Strings from stdin.
   * @deprecated For more consistency, use {@link #readAllStrings()}
   */
  @Deprecated public static String[] readStrings() {
    return readAllStrings();
  }
  /**
   * Redirect to a file.  This is a hack to get programs to work easily in eclipse.
   * (Added by James Riely 2012/01/12.)
   */
  public static void fromFile(String filename) {
    try {
      StdIn.scanner = new Scanner(new BufferedInputStream(new FileInputStream (filename)), StdIn.charsetName);
    } catch (FileNotFoundException e) {
      throw new Error (e.getMessage ());
    }
    StdIn.scanner.useLocale(StdIn.usLocale);
  }
  /**
   * Redirect to a string.  This is a hack to get programs to work easily in eclipse.
   * (Added by James Riely 2012/01/12.)
   */
  public static void fromString(String s) {
    StdIn.scanner = new Scanner(s);
    StdIn.scanner.useLocale(StdIn.usLocale);
  }
  /**
   * Interactive test of basic functionality.
   */
  public static void main(String[] args) {
    System.out.println("Type a string: ");
    String s = StdIn.readString();
    System.out.println("Your string was: " + s);
    System.out.println();
    System.out.println("Type an int: ");
    int a = StdIn.readInt();
    System.out.println("Your int was: " + a);
    System.out.println();
    System.out.println("Type a boolean: ");
    boolean b = StdIn.readBoolean();
    System.out.println("Your boolean was: " + b);
    System.out.println();
    System.out.println("Type a double: ");
    double c = StdIn.readDouble();
    System.out.println("Your double was: " + c);
    System.out.println();
  }
}
 |