/* * The java program that reads an input file input.txt * and then reads the contens of file. * the program throws file not found exception if file not found. * The program throws DimensionMismatchException if mismatch of dimension. * The program throws NumberFormatException if mismatch of data read. * */ //FormatChecker.java import java.io.File; import java.io.FileNotFoundException; import java.util.Scanner; public class FormatChecker { public static void main(String[] args) { String fileName=\"input.txt\"; //calling checkFormat checkFormat(fileName); } //Method checkFormat that takes string and check for //errors otherwise reads the file and print to console private static void checkFormat(String fileName) { Scanner filescanner=null; try { //thorws FileNotFoundException error if file not found filescanner=new Scanner(new File(fileName)); } catch (FileNotFoundException e) { System.out.println(e.getMessage()); } int rows=0; int cols=0; double[][] arr; String dimensions[]=filescanner.nextLine().split(\" \"); try { //thorws DimensionMismatchException error if mismatch of dimension if(dimensions.length>2) throw new DimensionMismatchException(\"Dimension mismatch\"); } catch (DimensionMismatchException e1) { e1.printStackTrace(); } try { //thorws NumberFormatException error if mismatch of reading values rows=Integer.parseInt(dimensions[0]); cols=Integer.parseInt(dimensions[1]); arr=new double[rows][cols]; System.out.println(\"File contents\"); for (int i = 0; i < arr.length; i++) { for (int j = 0; j < arr[i].length; j++) { arr[i][j]=filescanner.nextDouble(); System.out.printf(\"%6.1f\",arr[i][j]); } System.out.println(); } } catch (NumberFormatException e) { System.out.println(e.getMessage()); } } } ------------------------------------------------------------------------ //User defined DimensionMismatchException class //that extends the Exception class //DimensionMismatchException.java public class DimensionMismatchException extends Exception { public DimensionMismatchException(String msg) { super(msg); } } ------------------------------------------------------------------------ input.txt 5 6 2.5 0 1 0 0 0 0 -1 4 0 0 0 0 0 0 0 1.1 0 0 2 0 0 5 0 0 -3.14 0 0 0 0 ------------------------------------------------------------------------ File contents 2.5 0.0 1.0 0.0 0.0 0.0 0.0 -1.0 4.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 1.1 0.0 0.0 2.0 0.0 0.0 5.0 0.0 0.0 -3.1 0.0 0.0 0.0 0.0 Note : DimensionMismatchException class is included Solution /* * The java program that reads an input file input.txt * and then reads the contens of file. * the program throws file not found exception if file not found. * The program throws DimensionMismatchException if mismatch of dimension. * The program throws NumberFormatException if mismatch of data read. * */ //FormatChecker.java import java.io.File; import java.io.FileNotFoundException; import java.util.Scanner; public class FormatChecker { public static void main(String[] args) { String fileName=\"input.txt\"; //calling check.