程序保持循环。例外类?

时间:2017-03-02 06:34:07

标签: java exception

对于类我应该编写一个用银行帐户数据解析文件的程序,我必须编写一个异常类。银行帐号必须是10位数字,人名必须只有字母字符。我遇到的问题是,当我在程序中输入文件时,没有任何反应,并且它一直在询问文件。我也不知道如何编写异常类。为什么我的主要方法不能正常工作?我该如何编写异常类?

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();
}
}

1 个答案:

答案 0 :(得分:1)

在你的异常类中,只是将异常消息传递给它的超类super(exception),你需要使用自定义消息抛出异常,并在catch语句中对print stacktrace这样的异常做一些事情。 对于您的其他问题,我认为您没有在输入中给出完整的文件路径或文件名错误,其在文件未找到的循环捕获异常cluase。在末尾使用finally块

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;
}
}