Chat Zalo Chat Messenger Phone Number Đăng nhập
Class StdIn - Introduction to Programming in Java

Class StdIn – Introduction to Programming in Java

overview. The StdIn class provides static methods for reading strings and standard input numbers. These functions are divided into one of four categories:

those to read individual standard input tokens, one at a time, and convert each into a number, string, or Boolean those to read standard input characters, one at a time those to read

    standard input lines,

  • one at a time those to read a
  • sequence of values of the same

  • standard input type

  • ,
  • and return the values in an array

Generally, it is better not to mix functions from the different categories in the same program

.

Get started. To use this class, you must have StdIn.class in the Java classpath. If you used our auto-installer, you should be all set. Otherwise, download stdlib.jar and add to your Java classpath or download StdIn.java and place a copy in your working directory.

Reading standard input tokens and converting to numbers and strings. You can use the following methods to read numbers, strings, and standard input Booleans one at a time:

  • isEmpty
  • ()

  • readInt() readDouble() readString
  • () readShort()

  • readLong
  • ()

  • readFloat() readByte
  • ()

  • readBoolean
  • ()

The first method returns true if the standard input has no more tokens. Each other method ignores any entries that are blank. It then reads the following token and attempts to convert it to a value of the specified type. If successful, it returns that value; otherwise, it throws an InputMismatchException.

White space includes spaces, tabs, and new lines; the full definition inherits from Character.isWhitespace(char). A token is a maximum sequence of characters with no blank spaces. The precise rules for describing which tokens can be converted to integers and floating-point numbers are inherited from Scanner, using the Locale.US locale; the rules for floating-point numbers are slightly different from those for Double.valueOf(String), but are unlikely to worry most programmers.

As an example, the following code snippet reads the integers of the standard input, one at a time, and prints them one per line.

while (! StdIn.isEmpty()) { double value = StdIn.readDouble(); StdOut.println(value); }

Reading standard input characters. You can use the following two methods to read the characters from the standard input one at a time:

  • hasNextChar
  • ()

  • readChar()

The first method returns true if the standard input has more input (including white space). The second method reads and returns the next input character in the standard input (possibly a whitespace character).

As an example, the following code snippet reads characters from the standard input, one character at a time, and prints it to the standard output

. while (StdIn.hasNextChar()) { char c = StdIn.readChar(); StdOut.print(c); }

Reading standard input lines. You can use the following two methods to

read standard input lines:

  • hasNextLine
  • ()

  • readLine()

The first method returns true if the standard input has more input (including white space). The second method reads and returns the remaining portion of the next input line in the standard input (possibly white space), discarding the trailing line separator.

A line separator is defined as one of the following strings: n (Linux), r (Old Macintosh), rn (Windows), u2028, u2029, or u0085

. As an example, the

following code snippet reads the text from the standard input, one line at a time, and prints it to the standard output

. while (StdIn.hasNextLine()) { String line = StdIn.readLine(); StdOut.println(Line); }

Read a sequence of values of the same type from the standard input. You can use the following methods to read sequence numbers, strings, or Booleans (all of the same

type) from the standard input:

    readAllDoubles() readAllInts()

  • readAllLongs
  • () readAllStrings() readAllLines( )

  • readAll(
  • )
  • The

first three methods read all the remaining token in the standard entry and convert the tokens to values of the specified type, as in the corresponding readDouble. readInt, and readString() methods. The readAllLines() method reads all remaining lines in the standard input and returns them as an array of strings. The readAll() method reads all remaining input in the standard input and returns it as a string.

As an example, the following code snippet reads all the remaining tokens from the standard input and returns them as an array of strings.

String[] words = StdIn.readAllStrings();

Differences with the scanner. StdIn and Scanner are designed to parse tokens and convert them into primitive types and strings. The main differences are summarized below:

  • StdIn is a set of static methods and reads input readings only from standard input. It is suitable for use before a programmer knows the objects. See In for an object-oriented version that handles input for files, URLs, and sockets.
  • StdIn uses white space as a bounding pattern that separates tokens. The scanner supports arbitrary delimiter patterns.
  • StdIn forces the character set encoding to

  • UTF-8, which is the most commonly used character encoding for Unicode
  • . StdIn

  • forces the locale to Locale.US, for consistency with StdOut, Double.parseDouble(String), and floating-point literals. StdIn
  • has convenient methods for reading a single character; read in sequences of integers, doubles, or strings; and read in all remaining input.

Historical note: StdIn preceded Scanner; when Scanner was introduced, this class was reimplemented to use Scanner.

Use of standard input. Standard input is a fundamental abstraction of the operating system in Mac OS X, Windows, and Linux. The methods in StdIn are blocking, which means they will wait until you enter the entry in the standard entry. If the program has a loop that repeats until the standard input is empty, it must indicate that the entry has ended. To do this, depending on the operating system and IDE, use <Ctrl-d> or <Ctrl-z>, on your own line. If you’re redirecting the standard entry from a file, you won’t have to do anything to indicate that the entry has ended.

Known bugs. Java UTF-8 encoding does not recognize the optional byte order mask. If the entry begins with the optional byte-order mask, StdIn will have an additional uFEFF character at the beginning.

reference. For additional documentation, see Section 1.5 Computer Science: An Interdisciplinary Approach by Robert Sedgewick and Kevin Wayne.

Contact US