Cody Zeller
Cody Zeller

Reputation: 21

Program keeps looping. Exception class?

For class I am supposed to write a program that parses a file with bank account data, and I have to write an exception class. The bank account number has to be a 10 digit number, and the persons name has to only have alphabetic characters. The problem I am having is that when I input the file in the program, nothing happens, and it keeps asking for a file. I also don't know how to write the exception class. Why isn't my main method working properly? How would I write the exception class?

BankAccountProcessor.java

// import statements
import java.io.*;
import java.util.Scanner;
import java.util.StringTokenizer;

public class BankAccountProcessor {

// a main method that throws a FileNotFoundException
public static void main (String[] args) throws FileNotFoundException {

    // a variable that continues the program
    boolean runProgram = true;
    Scanner input = new Scanner(System.in);
    String fileName;

    // a while loop that runs only if runProgram = true
    System.out.println("What file would you like to parse?");
    fileName = input.next();
    File file = new File(fileName);
    while (runProgram) {
        try {
            Scanner inputFile = new Scanner(file);
            while (inputFile.hasNext()){
                String accountLine = inputFile.nextLine();
                if (BankAccountProcessor.isValid(accountLine) == true){
                    System.out.println("Line " + accountLine + " has been processed.");
                }
                runProgram = false;
        }
    }
    catch (FileNotFoundException e) {
        System.out.println("That file does not exist");
    }
    catch (BankAccountException e) {
        }
    }
}

public static boolean isValid(String accountLine) throws BankAccountException {
    StringTokenizer stringToken = new StringTokenizer(accountLine, ";");
    String tokenOne = stringToken.nextToken();
    String tokenTwo = stringToken.nextToken();
    if (stringToken.countTokens() != 2){
        throw new BankAccountException("Invalid Bank Account Info");
    }
    else if (tokenOne.length() != 10){
        throw new BankAccountException("Invalid Bank Account Info: Account Number is not 10 digits.");
    }
    else if (tokenTwo.length() < 3){
        throw new BankAccountException("Invalid Bank Account Info: Name must be more than 3 letters.");
    }
    else if (BankAccountProcessor.hasLetter(tokenOne) == true){
        throw new BankAccountException("Invalid Bank Account Info: Account Number must be all digits.");
    }
    else if (BankAccountProcessor.hasDigit(tokenTwo) == true){
        throw new BankAccountException("Invalid Bank Account Info: Account Name cannot have digits.");
    }
    return true;
}

// a method to check to see if the file has a digit
private static boolean hasDigit(String str){
    for (char c : str.toCharArray()){
        if (Character.isDigit(c)){
            return true;
        }
    }
    return false;
}

// a method to check to see if the file has a letter
private static boolean hasLetter(String str){
    for (char c : str.toCharArray()){
        if (Character.isLetter(c)){
            return true;
        }
    }
    return false;
}
}

BankAccountException.java

public class BankAccountException extends Exception {

// constructor
public BankAccountException(String exception) {
    super();
}
}

Upvotes: 0

Views: 151

Answers (1)

rakesh
rakesh

Reputation: 4498

In your exception class just pass exception message to its super class super(exception), you need to throw your exception with a custom message and in your catch statement do something with that exception like print stacktrace. For your other issue I think you are not giving full file path or file name wrong at the input, its looping at file not found catch exception cluase. Use a finally block at the end

while (runProgram) {
    try {
        Scanner inputFile = new Scanner(file);
        while (inputFile.hasNext()){
            String accountLine = inputFile.nextLine();
            if (BankAccountProcessor.isValid(accountLine) == true){
                System.out.println("Line " + accountLine + " has been processed.");
            }
            runProgram = false;
    }
}
catch (FileNotFoundException e) {
    System.out.println("That file does not exist");
}
catch (BankAccountException e) {
    }
 finally{
 runProgram =false;
}
}

Upvotes: 1

Related Questions