Question: Hello! I need help writing the following Java program: It should be a total of two methods, fulfilling all of the following requirements: For this
Hello! I need help writing the following Java program: It should be a total of two methods, fulfilling all of the following requirements:
For this assignment, you will create an executable program that reads in the contents of a given file and ensures that the data is formatted correctly.
Create a class called FormatChecker:
Your FormatChecker class should include 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 four exceptions: a FileNotFoundException, an InputMismatchException, a NumberFormatException, or a DimensionMismatchException.
Because FormatChecker is executable, your class should include a main method called the checkFormat method and handles the thrown exceptions. It should also handle any command-line arguments.
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 will cause one of these exceptions 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 an InputMismatchException or NumberFormatException, depending on how you're reading in the data from the file. It doesn't matter which of these your class throws.
If there are fewer rows and/or columns of data or more rows and/or columns of data than specified, it should throw a DimensionMismatchException. It should also throw this exception if there are more than two dimensions.
Some of these exceptions will be thrown automatically by classes from the Java API:
The FileNotFoundException is a checked exception provided by the Java API.
InputMismatchException and NumberFormatException are also provided by the Java API, but they're unchecked exceptions.
One of the exceptions you will have to throw yourself:
The DimensionMismatchException, which was created for this assignment, is unchecked and I've included the code for it below:
/**
* Runtime exception thrown when the
* given dimensions do not match the data.
*
* @author CS 221
*/
public class DimensionMismatchException extends RuntimeException
{
private static final long serialVersionUID = 1173110179989747752L;
/**
* Default constructor.
*/
public DimensionMismatchException()
{
super();
}
/**
* Constructor with given message.
* @param message - String
*/
public DimensionMismatchException(String message)
{
super(message);
}
}
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
