Question
Please help with this code in Java! Create a class called FormatChecker: Your FormatChecker class will contain a public static method called checkFormat that takes
Please help with this code in Java!
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.
I'm having issues with 3 errors in my code. two of the error checks are testing for testDimensionMismatch and the other error is testing for testValidFiles.
The first testDimensionMismatch error is testing using:
1 2 3 4 5
I think I may have to make another if statement that checks if row_colums[2] (or above) is null or not, but I can't figure it out.
The second testDimensionMismatch error is testing using:
1 2 3 4 5 6
I think this might be be solved by the same if statement, but again I can't figure this out.
The third error (testValidFiles) java.lang.NumberFormatException: For input string: "" :
3 4 1 2 3 4 5 6 7 8 9 10 11 12
I can't figure out exactly why I'm getting an error with this if I had to guess I would think it's something to due with white space, but I'm not sure.
Here is my code Thanks in advance:
import java.io.File; import java.io.FileNotFoundException; import java.util.Scanner;
public class FormatChecker {
public static void checkFormat(String fileName) throws FileNotFoundException {
// File object and scanner to read files File file = new File(fileName); Scanner scan = new Scanner(file);
// Reads first line of file String firstLine = ""; if (scan.hasNext()) {
firstLine = scan.nextLine(); }
// Separates by white space String row_columns[] = firstLine.split(" ");
// Gets numbers of rows and columns int rows = Integer.parseInt(row_columns[0]); int columns = Integer.parseInt(row_columns[1]);
// Number of rows int row_count = 0;
// Loops the entire file while (scan.hasNext()) {
// Reads line thats split by whitespace String line = scan.nextLine(); String[] numbers = line.split(" ");
// Will throw Dimension Exception if there is an invalid number of columns, else // parses values as doubles. if (numbers.length != columns) {
throw new DimensionMismatchException();
} else {
for (int i = 0; i < numbers.length; i++) {
Double.parseDouble(numbers[i]); } } // Increments row count row_count++;
} // Throws Dimension Exception if file has more date to read or rows does not // equal row_count if (scan.hasNext() || row_count != rows) {
throw new DimensionMismatchException(); } }
public static void main(String[] args) {
// Prompts the user to for a file name and receives it. Scanner scan = new Scanner(System.in); System.out.print("Please enter name of file: "); String filename = scan.nextLine();
try {
// Uses checkFormat constructor to check the format of the file checkFormat(filename);
System.out.println("File is in valid format");
} catch (FileNotFoundException e) {
System.out.println("FileNotFoundException: " + e);
} catch (NumberFormatException e) {
System.out.println("NumberFormatException: " + e);
} catch (DimensionMismatchException e) {
System.out.println("DimensionMismatchException: " + e);
} }
}
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started