Please help with this code Create a class called FormatCheck

Please help with this code!

Create a class called FormatChecker:

Your FormatChecker class will contain a public static method called checkFormat that takes as a parameter a String representing the name of a file and has no return type. This method will determine whether the contents of this file are in the correct format.

If the file can be found and the contents are in the correct format, the checkFormat method will return without an exception.

If the file can\'t be found or the contents are not in the correct format, the method will throw one of three exceptions: a FileNotFoundException, aNumberFormatException, or a DimensionMismatchException.


The valid format rules are as follows:
The first row contains two white-space-separated, positive integers.

The first integer specifies the number of rows in a grid.

The second integer specifies the number of columns in the grid.

Each subsequent row represents one row of the grid and should contain exactly one white-space-separated double value for each grid column.


This is an example of a correctly formatted file:

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



Any one of the following errors will make the format invalid and cause the given exception to be thrown:

If the file can\'t be found, the checkFormat method should throw a FileNotFoundException.

If the number format of the value in the file doesn\'t match the expected data type, it should throw a NumberFormatException.

If there are fewer rows and/or columns of data or more rows and/or columns of data than specified, it should throw aDimensionMismatchException. It should also throw this exception if there are more than two dimensions.

Some of these exceptions will be thrown by classes from the Java API that you use in your FormatChecker class. One of them you will have to throw yourself. The FileNotFoundException is a checked exception provided by the Java API. The NumberFormatException is also provided by the Java API, but it\'s an unchecked exception.

Thank you p.s. here is what I have so far; I am still having issues detecting dimension mismatches, one of the failures is at the bottom

import java.io.File;

import java.io.FileNotFoundException;

import java.util.Scanner;

public class FormatChecker {

   @SuppressWarnings(\"resource\")

   public static void checkFormat(String fileName) throws FileNotFoundException {

       Scanner filescanner;

       try {

           File file= new File(fileName);

           filescanner = new Scanner(file);

       } catch (FileNotFoundException e) {

           throw new FileNotFoundException();

       }

       int rows = 0;

       int cols = 0;

       double[][] arr;

       String dimensions[] = filescanner.nextLine().split(\" \");

       try {

           if (dimensions.length > 2){

               throw new DimensionMismatchException();

               }

       } catch (DimensionMismatchException e1) {

           filescanner.close();

           throw new DimensionMismatchException();

       }

       try {

           rows = Integer.parseInt(dimensions[0]);

           cols = Integer.parseInt(dimensions[1]);

           arr = new double[rows][cols];

           for (int i = 0; i < arr.length; i++) {

               for (int j = 0; j < arr[i].length; j++) {

                   arr[i][j] = filescanner.nextDouble();

               }

           }

       } catch (NumberFormatException e) {

           filescanner.close();

           throw new NumberFormatException();

       }

       filescanner.close();

   }

}

}      

failed test

4 3

1 2 3

4 5 6 7

8 9 10

11 12 13

and

3 3

1 2 3

4 5 6

7 8 9

10 11 12

Solution

Hi, Please find my code.

Please let me know in case of any issue.

import java.io.File;

import java.io.FileNotFoundException;

import java.util.Scanner;

public class FormatChecker {

  

   public static void main(String[] args) throws FileNotFoundException, DimensionMismatchException {

       try{

           checkFormat(\"inputData.txt\");

       }catch (Exception e) {

           System.out.println(e.getMessage());

       }

   }

   @SuppressWarnings(\"resource\")

   public static void checkFormat(String fileName) throws FileNotFoundException, DimensionMismatchException {

       Scanner filescanner;

       try {

           File file= new File(fileName);

           filescanner = new Scanner(file);

       } catch (FileNotFoundException e) {

           throw new FileNotFoundException();

       }

       int rows = 0;

       int cols = 0;

       double[][] arr;

       String dimensions[] = filescanner.nextLine().split(\"\\\\s+\");

       try {

           if (dimensions.length > 2){

               throw new DimensionMismatchException(\"Dimension Mismatch Excdption\");

           }

       } catch (DimensionMismatchException e1) {

           filescanner.close();

           throw new DimensionMismatchException(\"Dimension Mismatch Excdption\");

       }

       try {

           rows = Integer.parseInt(dimensions[0]);

           cols = Integer.parseInt(dimensions[1]);

           int fileRow = 0;

           String line = \"\";

           while(filescanner.hasNextLine()){

               line = filescanner.nextLine();

               String[] currentRow = line.split(\" \");

               if(currentRow.length > cols)

                   throw new DimensionMismatchException(\"Dimension Mismatch Excdption\");

              

               // checking all column elements

               for (int i = 0; i < currentRow.length; i++) {

                   Double.parseDouble(currentRow[i]);

               }

               fileRow++;

           }

          

           System.out.println(fileRow);

          

           if(fileRow < rows)

               throw new DimensionMismatchException(\"Dimension Mismatch Excdption\");

          

       } catch (NumberFormatException e) {

           filescanner.close();

           throw new NumberFormatException();

       }

       filescanner.close();

   }

}

class DimensionMismatchException extends Exception{

   public DimensionMismatchException(String message) {

       super(message);

   }

}

Please help with this code! Create a class called FormatChecker: Your FormatChecker class will contain a public static method called checkFormat that takes as a
Please help with this code! Create a class called FormatChecker: Your FormatChecker class will contain a public static method called checkFormat that takes as a
Please help with this code! Create a class called FormatChecker: Your FormatChecker class will contain a public static method called checkFormat that takes as a
Please help with this code! Create a class called FormatChecker: Your FormatChecker class will contain a public static method called checkFormat that takes as a
Please help with this code! Create a class called FormatChecker: Your FormatChecker class will contain a public static method called checkFormat that takes as a

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site