gpt4 book ai didi

java - 程序一直循环。异常类?

转载 作者:行者123 更新时间:2023-12-01 08:48:17 24 4
gpt4 key购买 nike

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

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

最佳答案

在您的异常类中,只需将异常消息传递给其父类(super class)super(exception),您需要使用自定义消息抛出异常,并在您的catch语句中对该异常执行一些操作,例如打印堆栈跟踪。对于您的其他问题,我认为您在输入时没有给出错误的完整文件路径或文件名,它在文件未找到捕获异常子句处循环。在最后使用finally block

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

关于java - 程序一直循环。异常类?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42548372/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com