Class InputReader

java.lang.Object
org.ddolib.util.io.InputReader

public class InputReader extends Object
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 Details

    • InputReader

      public InputReader(String file)
      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

      public Integer getInt() throws RuntimeException
      Reads the next integer from the input.
      Returns:
      the next integer
      Throws:
      RuntimeException - if the end of file is reached or parsing fails
    • getDouble

      public double getDouble() throws RuntimeException
      Reads the next double from the input.
      Returns:
      the next double
      Throws:
      RuntimeException - if the end of file is reached or parsing fails
    • getDoubleMatrix

      public double[][] getDoubleMatrix(int n, int m) throws RuntimeException
      Reads a matrix of doubles with the given dimensions.
      Parameters:
      n - number of rows
      m - number of columns
      Returns:
      a 2D array containing the matrix
      Throws:
      RuntimeException - if reading fails
    • getIntMatrix

      public int[][] getIntMatrix(int n, int m) throws RuntimeException
      Reads a matrix of integers with the given dimensions.
      Parameters:
      n - number of rows
      m - number of columns
      Returns:
      a 2D array containing the matrix
      Throws:
      RuntimeException - if reading fails
    • getIntLine

      public Integer[] getIntLine() throws RuntimeException
      Reads a full line of integers.
      Returns:
      an array of integers representing the line
      Throws:
      RuntimeException - if reading fails
    • skipLine

      public void skipLine() throws RuntimeException
      Skips the next line in the input.
      Throws:
      RuntimeException - if end of file is reached or reading fails
    • getString

      public String getString() throws RuntimeException
      Reads the next token as a string.
      Returns:
      the next token
      Throws:
      RuntimeException - if end of file is reached