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).

  • Constructor Summary Link icon

    Constructors
    Constructor
    Description
    In()
    Create an input stream from standard input.
    In(File file)
    Create an input stream from a file.
    Create an input stream from a filename or web page name.
    In(Socket socket)
    Create an input stream from a socket.
    In(URL url)
    Create an input stream from a URL.
    In(Scanner scanner)
    Create an input stream from a given Scanner source; use with new Scanner(String) to read from a string.
  • Method Summary Link icon

    Modifier and Type
    Method
    Description
    void
    Close the input stream.
    boolean
    Does the input stream exist?
    boolean
    Return true if the next value from the input stream can be interpreted as a byte
    boolean
    Is the input empty (including whitespace)? Use this to know whether the next call to readChar() will succeed.
    boolean
    Return true if the next value from the input stream can be interpreted as an double
    boolean
    Return true if the next value from the input stream can be interpreted as an float
    boolean
    Return true if the next value from the input stream can be interpreted as an int
    boolean
    Does the input have a next line? Use this to know whether the next call to readLine() will succeed.
    boolean
    Return true if the next value from the input stream can be interpreted as a long
    boolean
    Is the input empty (except possibly for whitespace)? Use this to know whether the next call to readString(), readDouble(), etc will succeed.
    static void
    main(String[] args)
    Test client.
    Read and return the remainder of the input as a string.
    double[]
    Read all doubles until the end of input is reached, and return them.
    int[]
    Read all ints until the end of input is reached, and return them.
    Read all strings until the end of input is reached, and return them.
    boolean
    Read and return the next boolean, allowing case-insensitive "true" or "1" for true, and "false" or "0" for false.
    byte
    Read and return the next byte.
    char
    Read and return the next character.
    double
    Read and return the next double.
    static double[]
    Deprecated.
    Clearer to use StdIn.readAllDoubles()
    static double[]
    readDoubles(String filename)
    Deprecated.
    Clearer to use new In(filename).readAllDoubles()
    float
    Read and return the next float.
    int
    Read and return the next int.
    static int[]
    Deprecated.
    Clearer to use StdIn.readAllInts()
    static int[]
    readInts(String filename)
    Deprecated.
    Clearer to use new In(filename).readAllInts()
    Read and return the next line.
    long
    Read and return the next long.
    short
    Read and return the next short.
    Read and return the next string.
    static String[]
    Deprecated.
    Clearer to use StdIn.readAllStrings()
    static String[]
    readStrings(String filename)
    Deprecated.
    Clearer to use new In(filename).readAllStrings()

    Methods inherited from class java.lang.Object Link icon

    equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Constructor Details Link icon

    • In Link icon

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

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

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

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

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

      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 Link icon

    • exists Link icon

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

      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 Link icon

      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 Link icon

      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 Link icon

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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