2017-03-02 5 views
0

Für Klasse soll ich ein Programm schreiben, das eine Datei mit Bankkontodaten analysiert, und ich muss eine Ausnahmeklasse schreiben. Die Bankkontonummer muss eine 10-stellige Nummer sein und der Personenname darf nur alphabetische Zeichen haben. Das Problem, das ich habe, ist, dass nichts passiert, wenn ich die Datei in das Programm eingabe, und es fragt nach einer Datei. Ich weiß auch nicht, wie ich die Ausnahmeklasse schreiben soll. Warum funktioniert meine Hauptmethode nicht richtig? Wie würde ich die Ausnahmeklasse schreiben?Programm läuft weiter. Ausnahmeklasse?

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

Für Ihre 'Exception' Klasse hat einen [at aussehen] (http://stackoverflow.com/questions/1070590/how-can-i-write-custom-exceptions). Bei anderen Problemen mit dem Scanner bedeutet "nichts passiert". –

+0

Wenn ich meine Hauptmethode ausführe und eine Textdatei mit Bankkonten eingib, läuft das Programm weiter und fragt, welche Datei ich analysieren möchte. –

Antwort

1

In Ihrer Ausnahmeklasse passieren nur Ausnahmemeldung zu seiner Superklasse super(exception), müssen Sie mit einem Ihrer Ausnahme werfen benutzerdefinierte Nachricht und in Ihrer catch-Anweisung tun etwas mit dieser Ausnahme wie print stacktrace. Für Ihr anderes Problem, ich denke, Sie geben nicht vollständige Dateipfad oder Dateiname falsch an der Eingabe, die Schleife bei Datei nicht gefunden catch Ausnahme Ausnahme. Verwenden Sie schließlich einen Block am Ende

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