Class In


  • public final class In
    extends Object
    The In data type 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 Computer Science: 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 delimiter, 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).

    Author:
    David Pritchard, Robert Sedgewick, Kevin Wayne
    • Constructor Summary

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

      All Methods Static Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      void close()
      Closes this input stream.
      boolean exists()
      Returns true if this input stream exists.
      boolean hasNextChar()
      Returns true if this input stream has more input (including whitespace).
      boolean hasNextLine()
      Returns true if this input stream has a next line.
      boolean isEmpty()
      Returns true if input stream is empty (except possibly whitespace).
      static void main​(String[] args)
      Unit tests the In data type.
      String readAll()
      Reads and returns the remainder of this input stream, as a string.
      double[] readAllDoubles()
      Reads all remaining tokens from this input stream, parses them as doubles, and returns them as an array of doubles.
      int[] readAllInts()
      Reads all remaining tokens from this input stream, parses them as integers, and returns them as an array of integers.
      String[] readAllLines()
      Reads all remaining lines from this input stream and returns them as an array of strings.
      long[] readAllLongs()
      Reads all remaining tokens from this input stream, parses them as longs, and returns them as an array of longs.
      String[] readAllStrings()
      Reads all remaining tokens from this input stream and returns them as an array of strings.
      boolean readBoolean()
      Reads the next token from this input stream, parses it as a boolean (interpreting either "true" or "1" as true, and either "false" or "0" as false).
      byte readByte()
      Reads the next token from this input stream, parses it as a byte, and returns the byte.
      char readChar()
      Reads and returns the next character in this input stream.
      double readDouble()
      Reads the next token from this input stream, parses it as a double, and returns the double.
      float readFloat()
      Reads the next token from this input stream, parses it as a float, and returns the float.
      int readInt()
      Reads the next token from this input stream, parses it as a int, and returns the int.
      String readLine()
      Reads and returns the next line in this input stream.
      long readLong()
      Reads the next token from this input stream, parses it as a long, and returns the long.
      short readShort()
      Reads the next token from this input stream, parses it as a short, and returns the short.
      String readString()
      Reads the next token from this input stream and returns it as a String.
    • Method Detail

      • exists

        public boolean exists()
        Returns true if this input stream exists.
        Returns:
        true if this input stream exists; false otherwise
      • isEmpty

        public boolean isEmpty()
        Returns true if input stream is empty (except possibly whitespace). Use this to know whether the next call to readString(), readDouble(), etc. will succeed.
        Returns:
        true if this input stream is empty (except possibly whitespace); false otherwise
      • hasNextLine

        public boolean hasNextLine()
        Returns true if this input stream has a next line. Use this method to know whether the next call to readLine() will succeed. This method is functionally equivalent to hasNextChar().
        Returns:
        true if this input stream has more input (including whitespace); false otherwise
      • hasNextChar

        public boolean hasNextChar()
        Returns true if this input stream has more input (including whitespace). Use this method to know whether the next call to readChar() will succeed. This method is functionally equivalent to hasNextLine().
        Returns:
        true if this input stream has more input (including whitespace); false otherwise
      • readLine

        public String readLine()
        Reads and returns the next line in this input stream.
        Returns:
        the next line in this input stream; null if no such line
      • readChar

        public char readChar()
        Reads and returns the next character in this input stream.
        Returns:
        the next char in this input stream
        Throws:
        NoSuchElementException - if the input stream is empty
      • readAll

        public String readAll()
        Reads and returns the remainder of this input stream, as a string.
        Returns:
        the remainder of this input stream, as a string
      • readString

        public String readString()
        Reads the next token from this input stream and returns it as a String.
        Returns:
        the next String in this input stream
        Throws:
        NoSuchElementException - if the input stream is empty
      • readInt

        public int readInt()
        Reads the next token from this input stream, parses it as a int, and returns the int.
        Returns:
        the next int in this input stream
        Throws:
        NoSuchElementException - if the input stream is empty
        InputMismatchException - if the next token cannot be parsed as an int
      • readDouble

        public double readDouble()
        Reads the next token from this input stream, parses it as a double, and returns the double.
        Returns:
        the next double in this input stream
        Throws:
        NoSuchElementException - if the input stream is empty
        InputMismatchException - if the next token cannot be parsed as a double
      • readFloat

        public float readFloat()
        Reads the next token from this input stream, parses it as a float, and returns the float.
        Returns:
        the next float in this input stream
        Throws:
        NoSuchElementException - if the input stream is empty
        InputMismatchException - if the next token cannot be parsed as a float
      • readLong

        public long readLong()
        Reads the next token from this input stream, parses it as a long, and returns the long.
        Returns:
        the next long in this input stream
        Throws:
        NoSuchElementException - if the input stream is empty
        InputMismatchException - if the next token cannot be parsed as a long
      • readShort

        public short readShort()
        Reads the next token from this input stream, parses it as a short, and returns the short.
        Returns:
        the next short in this input stream
        Throws:
        NoSuchElementException - if the input stream is empty
        InputMismatchException - if the next token cannot be parsed as a short
      • readByte

        public byte readByte()
        Reads the next token from this input stream, parses it as a byte, and returns the byte.

        To read binary data, use BinaryIn.

        Returns:
        the next byte in this input stream
        Throws:
        NoSuchElementException - if the input stream is empty
        InputMismatchException - if the next token cannot be parsed as a byte
      • readBoolean

        public boolean readBoolean()
        Reads the next token from this input stream, parses it as a boolean (interpreting either "true" or "1" as true, and either "false" or "0" as false).
        Returns:
        the next boolean in this input stream
        Throws:
        NoSuchElementException - if the input stream is empty
        InputMismatchException - if the next token cannot be parsed as a boolean
      • readAllStrings

        public String[] readAllStrings()
        Reads all remaining tokens from this input stream and returns them as an array of strings.
        Returns:
        all remaining tokens in this input stream, as an array of strings
      • readAllLines

        public String[] readAllLines()
        Reads all remaining lines from this input stream and returns them as an array of strings.
        Returns:
        all remaining lines in this input stream, as an array of strings
      • readAllInts

        public int[] readAllInts()
        Reads all remaining tokens from this input stream, parses them as integers, and returns them as an array of integers.
        Returns:
        all remaining lines in this input stream, as an array of integers
      • readAllLongs

        public long[] readAllLongs()
        Reads all remaining tokens from this input stream, parses them as longs, and returns them as an array of longs.
        Returns:
        all remaining lines in this input stream, as an array of longs
      • readAllDoubles

        public double[] readAllDoubles()
        Reads all remaining tokens from this input stream, parses them as doubles, and returns them as an array of doubles.
        Returns:
        all remaining lines in this input stream, as an array of doubles
      • close

        public void close()
        Closes this input stream.
      • main

        public static void main​(String[] args)
        Unit tests the In data type.
        Parameters:
        args - the command-line arguments