Reputation: 55
I am writing an exception class for a project, and have become stumped with a problem. The class asks for a file name with bank accounts in it, reads the file, and checks if they meet certain criteria. If they do not meet one of these criteria, an error of type BankAccountException is thrown, which is a custom error class that simply extends
the Exception
class and is renamed. The problem I am coming across is that once I enter the name of the file, the program immediately asks for the name of another file. I have been sitting on this for a while and cannot figure it out, any help would be appreciated.
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;
}
}
Upvotes: 0
Views: 269
Reputation: 831
Try putting in some debug System.out.println()
statements to see how far you are getting in your code. That will help you narrow down where the problem lies.
Upvotes: 0
Reputation: 5537
If I had to guess (and I am guessing), I'd say that the file you are passing in lacks any tokens so while (inputFile.hasNext())
returns false and it asks you for your next file.
Upvotes: 0
Reputation: 178263
You are assigning true
to your runProgram
variable in each loop with the =
operator. The result is true
so your while loop will loop forever. Use the ==
operator to compare:
while (runProgram == true)
or to put it more simply,
while (runProgram)
Upvotes: 4