Java 程序不断循环。例外类?

Java 程序不断循环。例外类?,java,exception,Java,Exception,对于类,我应该编写一个程序来解析包含银行帐户数据的文件,并且我必须编写一个异常类。银行账号必须是10位数字,人员姓名必须只有字母字符。我遇到的问题是,当我在程序中输入文件时,什么都没有发生,并且它一直在请求一个文件。我也不知道如何编写异常类。为什么我的主要方法不能正常工作?如何编写异常类 BankAccountProcessor.java // import statements import java.io.*; import java.util.Scanner; import java.ut

对于类,我应该编写一个程序来解析包含银行帐户数据的文件,并且我必须编写一个异常类。银行账号必须是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;
}
}
public class BankAccountException extends Exception {

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

在exception类中,只需将异常消息传递给它的超类
super(exception)
,您需要抛出带有自定义消息的异常,并在catch语句中对该异常执行一些操作,如print stacktrace。 对于您的另一个问题,我认为您在输入时没有给出错误的完整文件路径或文件名,它在file not found处循环捕获异常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;
}
}

对于您的
异常
类,请使用。对于扫描仪的另一个问题,“什么都没发生”具体是什么意思。当我运行main方法并输入一个包含银行帐户的文本文件时,程序会不断循环并询问我要解析的文件。