程序循环而不运行完整代码

时间:2013-02-22 23:21:22

标签: java exception-handling

我正在为一个项目编写一个异常类,并且已经遇到了问题。该类要求在其中包含银行帐户的文件名,读取该文件,并检查它们是否符合特定条件。如果它们不符合这些条件之一,则抛出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;
  }


 }

3 个答案:

答案 0 :(得分:4)

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

while (runProgram == true)

或者更简单地说,

while (runProgram)

答案 1 :(得分:0)

如果我不得不猜测(我猜),我会说你传入的文件没有任何令牌,所以while (inputFile.hasNext())返回false并要求你提供下一个文件。

答案 2 :(得分:0)

尝试输入一些调试System.out.println()语句,以了解您在代码中获得了多少。这将帮助您缩小问题所在。