Class In
- Object
-
- In
-
public final class In extends Object
TheIndata 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 givenScannersource; use withnew Scanner(String)to read from a string.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description voidclose()Closes this input stream.booleanexists()Returns true if this input stream exists.booleanhasNextChar()Returns true if this input stream has more input (including whitespace).booleanhasNextLine()Returns true if this input stream has a next line.booleanisEmpty()Returns true if input stream is empty (except possibly whitespace).static voidmain(String[] args)Unit tests theIndata type.StringreadAll()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.booleanreadBoolean()Reads the next token from this input stream, parses it as aboolean(interpreting either"true"or"1"astrue, and either"false"or"0"asfalse).bytereadByte()Reads the next token from this input stream, parses it as abyte, and returns thebyte.charreadChar()Reads and returns the next character in this input stream.doublereadDouble()Reads the next token from this input stream, parses it as adouble, and returns thedouble.floatreadFloat()Reads the next token from this input stream, parses it as afloat, and returns thefloat.intreadInt()Reads the next token from this input stream, parses it as aint, and returns theint.StringreadLine()Reads and returns the next line in this input stream.longreadLong()Reads the next token from this input stream, parses it as along, and returns thelong.shortreadShort()Reads the next token from this input stream, parses it as ashort, and returns theshort.StringreadString()Reads the next token from this input stream and returns it as aString.
-
-
-
Constructor Detail
-
In
public In()
Initializes an input stream from standard input.
-
In
public In(Socket socket)
Initializes an input stream from a socket.- Parameters:
socket- the socket- Throws:
IllegalArgumentException- if cannot opensocketIllegalArgumentException- ifsocketisnull
-
In
public In(URL url)
Initializes an input stream from a URL.- Parameters:
url- the URL- Throws:
IllegalArgumentException- if cannot openurlIllegalArgumentException- ifurlisnull
-
In
public In(File file)
Initializes an input stream from a file.- Parameters:
file- the file- Throws:
IllegalArgumentException- if cannot openfileIllegalArgumentException- iffileisnull
-
In
public In(String name)
Initializes an input stream from a filename or web page name.- Parameters:
name- the filename or web page name- Throws:
IllegalArgumentException- if cannot opennameas a file or URLIllegalArgumentException- ifnameisnull
-
In
public In(Scanner scanner)
Initializes an input stream from a givenScannersource; use withnew 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.
- Parameters:
scanner- the scanner- Throws:
IllegalArgumentException- ifscannerisnull
-
-
Method Detail
-
exists
public boolean exists()
Returns true if this input stream exists.- Returns:
trueif this input stream exists;falseotherwise
-
isEmpty
public boolean isEmpty()
Returns true if input stream is empty (except possibly whitespace). Use this to know whether the next call toreadString(),readDouble(), etc. will succeed.- Returns:
trueif this input stream is empty (except possibly whitespace);falseotherwise
-
hasNextLine
public boolean hasNextLine()
Returns true if this input stream has a next line. Use this method to know whether the next call toreadLine()will succeed. This method is functionally equivalent tohasNextChar().- Returns:
trueif this input stream has more input (including whitespace);falseotherwise
-
hasNextChar
public boolean hasNextChar()
Returns true if this input stream has more input (including whitespace). Use this method to know whether the next call toreadChar()will succeed. This method is functionally equivalent tohasNextLine().- Returns:
trueif this input stream has more input (including whitespace);falseotherwise
-
readLine
public String readLine()
Reads and returns the next line in this input stream.- Returns:
- the next line in this input stream;
nullif no such line
-
readChar
public char readChar()
Reads and returns the next character in this input stream.- Returns:
- the next
charin 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 aString.- Returns:
- the next
Stringin 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 aint, and returns theint.- Returns:
- the next
intin this input stream - Throws:
NoSuchElementException- if the input stream is emptyInputMismatchException- if the next token cannot be parsed as anint
-
readDouble
public double readDouble()
Reads the next token from this input stream, parses it as adouble, and returns thedouble.- Returns:
- the next
doublein this input stream - Throws:
NoSuchElementException- if the input stream is emptyInputMismatchException- if the next token cannot be parsed as adouble
-
readFloat
public float readFloat()
Reads the next token from this input stream, parses it as afloat, and returns thefloat.- Returns:
- the next
floatin this input stream - Throws:
NoSuchElementException- if the input stream is emptyInputMismatchException- if the next token cannot be parsed as afloat
-
readLong
public long readLong()
Reads the next token from this input stream, parses it as along, and returns thelong.- Returns:
- the next
longin this input stream - Throws:
NoSuchElementException- if the input stream is emptyInputMismatchException- if the next token cannot be parsed as along
-
readShort
public short readShort()
Reads the next token from this input stream, parses it as ashort, and returns theshort.- Returns:
- the next
shortin this input stream - Throws:
NoSuchElementException- if the input stream is emptyInputMismatchException- if the next token cannot be parsed as ashort
-
readByte
public byte readByte()
Reads the next token from this input stream, parses it as abyte, and returns thebyte.To read binary data, use
BinaryIn.- Returns:
- the next
bytein this input stream - Throws:
NoSuchElementException- if the input stream is emptyInputMismatchException- if the next token cannot be parsed as abyte
-
readBoolean
public boolean readBoolean()
Reads the next token from this input stream, parses it as aboolean(interpreting either"true"or"1"astrue, and either"false"or"0"asfalse).- Returns:
- the next
booleanin this input stream - Throws:
NoSuchElementException- if the input stream is emptyInputMismatchException- if the next token cannot be parsed as aboolean
-
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 theIndata type.- Parameters:
args- the command-line arguments
-
-