0

我正在为一个项目编写一个异常类,并且遇到了一个问题。该类要求一个包含银行帐户的文件名,读取该文件,并检查它们是否符合某些条件。如果它们不满足这些条件之一,则会引发 BankAccountException 类型的错误,这是一个自定义错误类,只是extends该类Exception并被重命名。我遇到的问题是,一旦我输入文件名,程序会立即询问另一个文件的名称。我已经坐了一段时间,无法弄清楚,任何帮助将不胜感激。

   import java.util.*;


 import java.io.*;

   public class BankAccountProcessor{


  public static void main(String[] args){
     boolean runProgram = true;
     Scanner input = new Scanner(System.in);
     String filename;

     while (runProgram = true){
        try{
           System.out.println("Please enter the name of the file you want to parse.");
           filename = input.next();
           File file = new File(filename);
           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){

           }
     }
  }

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

  private static boolean hasDigit(String str){
     for (char c : str.toCharArray()){
        if (Character.isDigit(c)){
           return true;
        }
     }
     return false;
  }

  private static boolean hasLetter(String str){
     for (char c : str.toCharArray()){
        if (Character.isLetter(c)){
           return true;
        }
     }
     return false;
  }


 }
4

3 回答 3

4

您正在使用运算符在每个循环中分配true您的变量。结果是你的while循环将永远循环。使用运算符进行比较:runProgram=true==

while (runProgram == true)

或者更简单地说,

while (runProgram)
于 2013-02-22T23:23:29.260 回答
0

试着加入一些调试System.out.println()语句,看看你的代码能走多远。这将帮助您缩小问题所在。

于 2013-02-22T23:43:20.707 回答
0

如果我不得不猜测(我在猜测),我会说您传入的文件缺少任何标记,因此while (inputFile.hasNext())返回 false 并要求您提供下一个文件。

于 2013-02-22T23:27:09.723 回答