Hello! I need help writing the following Java program: It should be a total of t
ID: 3751835 • Letter: H
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 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);
}
}
Explanation / Answer
Here is the completed code for this problem. Comments are included, go through it, learn how things work and let me know if you have any doubts. Thanks
// FormatChecker.java
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
public class FormatChecker {
/**
* method to check if the file is in valid format
*
* @param filename
* name of input file
* @throws FileNotFoundException
* if the file is not found
* @throws NumberFormatException
* if there is any numeric errors in the file
* @throws DimensionMismatchException
* if there are wrong number of rows/cols
*/
public static void checkFormat(String filename)
throws FileNotFoundException {
// defining a scanner to read file
Scanner scanner = new Scanner(new File(filename));
// reading first line
String firstLine = "";
if (scanner.hasNext()) {
firstLine = scanner.nextLine();
}
// splitting line by white space
String row_col[] = firstLine.split(" ");
// extracting number of rows,cols
int rows = Integer.parseInt(row_col[0]);
int cols = Integer.parseInt(row_col[1]);
int row_count = 0; // number of rows read
// loop until the end of file
while (scanner.hasNext()) {
// reading line, splitting by white space
String line = scanner.nextLine();
String[] numbers = line.split(" ");
if (numbers.length != cols) {
// invalid number of columns in current row
throw new DimensionMismatchException();
} else {
// trying to parse all values in this line as double
for (int i = 0; i < numbers.length; i++) {
Double.parseDouble(numbers[i]);
}
}
// incrementing row count
row_count++;
}
if (scanner.hasNext() || row_count != rows) {
// file has more data to read, or more or less than specified
throw new DimensionMismatchException();
}
}
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
// prompting and receiving a file name
System.out.print("Enter name of file to check: ");
String filename = scanner.nextLine();
try {
// checking format
checkFormat(filename);
// if the above line works without any exceptions,file is valid
System.out.println("File is in valid format");
} catch (FileNotFoundException e) {
System.out.println("File not found!");
} catch (NumberFormatException e) {
System.out
.println("NumberFormatException is occurred, file is not valid");
} catch (DimensionMismatchException e) {
System.out
.println("DimensionMismatchException is occurred, file is not valid");
}
}
}
/*OUTPUT (multiple runs using different input files)*/
Enter name of file to check: input1.txt
File is in valid format
Enter name of file to check: input2.txt
NumberFormatException is occurred, file is not valid
Enter name of file to check: input3.txt
DimensionMismatchException is occurred, file is not valid
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.