Package stdlib

Class In

java.lang.Object
stdlib.In

public final class In extends Object
Input. This class provides methods for reading strings and numbers from standard input, file input, URLs, and sockets.

The Locale used is: language = English, country = US. This is consistent with the formatting conventions with Java floating-point literals, command-line arguments (via Double.parseDouble(String)) and standard output.

For additional documentation, see Section 3.1 of Introduction to Programming in Java: An Interdisciplinary Approach by Robert Sedgewick and Kevin Wayne.

Like Scanner, reading a token also consumes preceding Java whitespace, reading a full line consumes the following end-of-line delimeter, while reading a character consumes nothing extra.

Whitespace is defined in Character.isWhitespace(char). Newlines consist of \n, \r, \r\n, and Unicode hex code points 0x2028, 0x2029, 0x0085; see Scanner.java (NB: Java 6u23 and earlier uses only \r, \r, \r\n).

  • Field Details

  • Constructor Details

    • In

      public In()
      Create an input stream from standard input.
    • In

      public In(Socket socket)
      Create an input stream from a socket.
    • In

      public In(URL url)
      Create an input stream from a URL.
    • In

      public In(File file)
      Create an input stream from a file.
    • In

      public In(String s)
      Create an input stream from a filename or web page name.
    • In

      public In(Scanner scanner)
      Create an input stream from a given Scanner source; use with new Scanner(String) to read from a string.

      Note that this does not create a defensive copy, so the scanner will be mutated as you read on.

  • Method Details

    • exists

      public boolean exists()
      Does the input stream exist?
    • isEmpty

      public boolean isEmpty()
      Is the input empty (except possibly for whitespace)? Use this to know whether the next call to readString(), readDouble(), etc will succeed.
    • hasNextLine

      public boolean hasNextLine()
      Does the input have a next line? Use this to know whether the next call to readLine() will succeed.

      Functionally equivalent to hasNextChar().

    • hasNextChar

      public boolean hasNextChar()
      Is the input empty (including whitespace)? Use this to know whether the next call to readChar() will succeed.

      Functionally equivalent to hasNextLine().

    • readLine

      public String readLine()
      Read and return the next line.
    • readChar

      public char readChar()
      Read and return the next character.
    • readAll

      public String readAll()
      Read and return the remainder of the input as a string.
    • readString

      public String readString()
      Read and return the next string.
    • readInt

      public int readInt()
      Read and return the next int.
    • readDouble

      public double readDouble()
      Read and return the next double.
    • readFloat

      public float readFloat()
      Read and return the next float.
    • readLong

      public long readLong()
      Read and return the next long.
    • readShort

      public short readShort()
      Read and return the next short.
    • readByte

      public byte readByte()
      Read and return the next byte.
    • readBoolean

      public boolean readBoolean()
      Read and return the next boolean, allowing case-insensitive "true" or "1" for true, and "false" or "0" for false.
    • readAllStrings

      public String[] readAllStrings()
      Read all strings until the end of input is reached, and return them.
    • readAllInts

      public int[] readAllInts()
      Read all ints until the end of input is reached, and return them.
    • readAllDoubles

      public double[] readAllDoubles()
      Read all doubles until the end of input is reached, and return them.
    • close

      public void close()
      Close the input stream.
    • readInts

      @Deprecated public static int[] readInts(String filename)
      Deprecated.
      Clearer to use new In(filename).readAllInts()
      Reads all ints from a file
    • readDoubles

      @Deprecated public static double[] readDoubles(String filename)
      Deprecated.
      Clearer to use new In(filename).readAllDoubles()
      Reads all doubles from a file
    • readStrings

      @Deprecated public static String[] readStrings(String filename)
      Deprecated.
      Clearer to use new In(filename).readAllStrings()
      Reads all strings from a file
    • readInts

      @Deprecated public static int[] readInts()
      Deprecated.
      Clearer to use StdIn.readAllInts()
      Reads all ints from stdin
    • readDoubles

      @Deprecated public static double[] readDoubles()
      Deprecated.
      Clearer to use StdIn.readAllDoubles()
      Reads all doubles from stdin
    • readStrings

      @Deprecated public static String[] readStrings()
      Deprecated.
      Clearer to use StdIn.readAllStrings()
      Reads all strings from stdin
    • hasNextInt

      public boolean hasNextInt()
      Return true if the next value from the input stream can be interpreted as an int
    • hasNextDouble

      public boolean hasNextDouble()
      Return true if the next value from the input stream can be interpreted as an double
    • hasNextFloat

      public boolean hasNextFloat()
      Return true if the next value from the input stream can be interpreted as an float
    • hasNextLong

      public boolean hasNextLong()
      Return true if the next value from the input stream can be interpreted as a long
    • hasNextByte

      public boolean hasNextByte()
      Return true if the next value from the input stream can be interpreted as a byte
    • main

      public static void main(String[] args)
      Test client.