1
\$\begingroup\$

This was my original posting: Check that a data file contains the expected number of rows and columns of integers

I updated the code. Still what could I improve on?

Another question I have is how come my last method fileContent() does not need any throws declaration in the method header?

import java.io.File;
import java.io.FileNotFoundException;
import java.util.InputMismatchException;
import java.util.Scanner;

public class FormatChecker2 {

    public static void main(String[] args) {
        FormatChecker2 tester = new FormatChecker2(); // To avoid every method being static.

        if (args.length == 0) {
            System.out.println("Usage: $ java FormatChecker file1 [file2 ... fileN]");
        } else {
            for (String fileName : args) {
                try {
                    tester.readInTextFile(fileName);
                    System.out.println(fileName + "");
                    System.out.println("VALID");
                    System.out.println();
                } catch (FileNotFoundException notFound) {
                    System.out.println(notFound.getMessage() + "");
                    System.out.println(notFound + " (The system cannot find the file specified)");
                    System.out.println("INVALID");
                    System.out.println();
                } catch (NumberFormatException notInt) {
                    System.out.println(fileName);
                    // String printMessage = notInt.toString().replaceAll("#.*?;", ""); "#" + file +
                    // ";"
                    System.out.println(notInt);
                    System.out.println("INVALID");
                    System.out.println();
                } catch (InputMismatchException badContent) {
                    System.out.println(fileName);
                    System.out.println(badContent);
                    System.out.println("INVALID");
                    System.out.println();
                } catch (IllegalCharacterException delChar) {
                    System.out.println(fileName);
                    System.out.println(delChar);
                    System.out.println("INVALID");
                    System.out.println();
                }

            }
        }
    }

    public void readInTextFile(String fileName)
            throws FileNotFoundException, IllegalCharacterException, NumberFormatException {
        File file = new File(fileName);
        if (file.exists() && file.isFile()) {
            parseFile(file);
        } else {
            throw new FileNotFoundException(fileName);
        }
    }

    public void parseFile(File file) throws FileNotFoundException, IllegalCharacterException {
        Scanner fileScan = new Scanner(file);
        String declaredRowCol = fileScan.nextLine().trim();
        String[] dimensions = declaredRowCol.split("\\s+");
        String declaredRowStr = dimensions[0];
        String declaredColStr = dimensions[1];
        int rowCount = 0;
        int colCount = 0;
        String notAllNumbers = "";
        while (fileScan.hasNextLine()) {
            String line = fileScan.nextLine().trim();
            if (!line.isEmpty()) {
                rowCount++;// Counts actual number of rows
            }
            Scanner lineScan = new Scanner(line);
            while (lineScan.hasNext()) {
                String token = lineScan.next().trim(); // should I include trim?
                char letterCheck = token.charAt(0);
                // Checks to make sure the content of this file includes only numbers
                if (Character.isLetter(letterCheck)) {
                    notAllNumbers = letterCheck + "";
                }
                colCount++; // Counts actual number of columns, divide by rowCount to get actual
            }
            lineScan.close();
        }
        // Checks to make sure there is not an extra integer on the first line
        if (dimensions.length > 2) {
            throw new IllegalCharacterException(
                    "Row and Column have already been provided on the first line of the file. The extra integer: "
                            + "\"" + dimensions[2] + "\"" + " should not be included.");
        } else if (!notAllNumbers.isEmpty()) {
            throw new IllegalCharacterException(
                    "This value in your file: " + "\"" + notAllNumbers + "\"" + " is not a number.");
        } else {
            formatDimensions(dimensions, declaredRowStr, declaredColStr, rowCount, colCount);
        }
    }

    public void formatDimensions(String[] dimensions, String declaredRowStr, String declaredColStr, int rowCount,
            int colCount) throws IllegalCharacterException, NumberFormatException, FileNotFoundException {

        int declaredRow = 0;
        int declaredCol = 0;
        // Catches if the variables on the first line are of type integer
        try {
            declaredRow = Integer.parseInt(declaredRowStr);
            declaredCol = Integer.parseInt(declaredColStr);
            fileContent(declaredRow, declaredCol, rowCount, colCount);
        } catch (NumberFormatException e) {
            throw new NumberFormatException("This value on the first line of your file:"
                    + e.getMessage().substring(17, e.getMessage().length()) + " is not of type integer.");
        }
    }

    public void fileContent(int declaredRow, int declaredCol, int rowCount, int colCount)
            throws FileNotFoundException, IllegalCharacterException {

        // Checks to see if row and column matches the actual number of rows and columns
        double roundToRealRowCount = (rowCount / 1.0); // This is to account for rounding of
                                                        // integer
        double roundToRealColCount = (colCount / roundToRealRowCount);// if row or column is not
                                                                        // divided evenly

        if (rowCount != declaredRow) {
            throw new InputMismatchException("Number of rows declaration: " + "'" + declaredRow + "'"
                    + " on first line does not match the actual number of rows in file.");
        } else if (roundToRealColCount != declaredCol) {
            throw new InputMismatchException("Number of columns declaration: " + "'" + declaredCol + "'"
                    + " on first line does not match the actual number of columns in file.");
        }
    }
}
\$\endgroup\$
2
  • \$\begingroup\$ ...but I see fileContent method does have throws clause .... \$\endgroup\$ Commented Sep 19, 2017 at 6:32
  • \$\begingroup\$ all the printing inside the catch clauses look very similar. should be put in a method. \$\endgroup\$ Commented Sep 19, 2017 at 6:34

1 Answer 1

1
\$\begingroup\$

You could use the split method in the String class to count the columns.

String[] columns = line.split(SEPARATOR);
int colCount = columns.length.
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.