Package org.ddolib.util.io
Class InputReader
java.lang.Object
org.ddolib.util.io.InputReader
Utility class to read formatted input from a file.
This class provides convenient methods to read integers, doubles, lines of integers,
matrices of numbers, and individual strings from a text file. It uses a
BufferedReader for efficient line-by-line reading and a StringTokenizer
to parse tokens from each line.
The methods throw a RuntimeException when an I/O error occurs or when
attempting to read beyond the end of the file.
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptiondoubleReads the next double from the input.double[][]getDoubleMatrix(int n, int m) Reads a matrix of doubles with the given dimensions.getInt()Reads the next integer from the input.Integer[]Reads a full line of integers.int[][]getIntMatrix(int n, int m) Reads a matrix of integers with the given dimensions.Reads the next token as a string.voidskipLine()Skips the next line in the input.
-
Constructor Details
-
InputReader
Constructs an InputReader for the specified file.- Parameters:
file- the path to the input file- Throws:
RuntimeException- if the file cannot be opened
-
-
Method Details
-
getInt
Reads the next integer from the input.- Returns:
- the next integer
- Throws:
RuntimeException- if the end of file is reached or parsing fails
-
getDouble
Reads the next double from the input.- Returns:
- the next double
- Throws:
RuntimeException- if the end of file is reached or parsing fails
-
getDoubleMatrix
Reads a matrix of doubles with the given dimensions.- Parameters:
n- number of rowsm- number of columns- Returns:
- a 2D array containing the matrix
- Throws:
RuntimeException- if reading fails
-
getIntMatrix
Reads a matrix of integers with the given dimensions.- Parameters:
n- number of rowsm- number of columns- Returns:
- a 2D array containing the matrix
- Throws:
RuntimeException- if reading fails
-
getIntLine
Reads a full line of integers.- Returns:
- an array of integers representing the line
- Throws:
RuntimeException- if reading fails
-
skipLine
Skips the next line in the input.- Throws:
RuntimeException- if end of file is reached or reading fails
-
getString
Reads the next token as a string.- Returns:
- the next token
- Throws:
RuntimeException- if end of file is reached
-