Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

can you fix the code so that it can display the correct output It need to display ( , ) comthing like this exaple two

can you fix the code so that it can display the correct output It need to display (,)comthing like this exaple two What file would you like to parse.
accountsFile.txt
Line 7536765490;Tatiana Harrison successfully processed
Line 7462887443;John Anvik successfully processed
Invalid Bank Account info : Account number has non-digit character: 984733122a
Continue?
n
Okay, quitting Example one
What file would you like to parse.
accountsFile.txt
Line 7536765490;Tatiana Harrison successfully processed
Line 7462887443;John Anvik successfully processed
Invalid Bank Account info : Account number has non-digit character: 984733122a
Continue?
y
Invalid Bank Account info : Invalid account number: 04712f643
Continue?
y
File has been parsed. file import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.Scanner;
public class BankAccountProcessor {
public static void main(String[] args){
Scanner scanner = new Scanner(System.in);
boolean runProgram = true;
while (runProgram){
try {
System.out.println("What file would you like to parse?");
String fileName = scanner.nextLine();
BufferedReader reader = new BufferedReader(new FileReader(fileName));
String line;
while ((line = reader.readLine())!= null){
try {
processLine(line);
} catch (BankAccountException e){
System.out.println(e.getMessage());
System.out.println("Continue?(y/n)");
String response = scanner.nextLine();
if (!response.equalsIgnoreCase("y")){
runProgram = false;
break;
}
}
}
reader.close();
if (runProgram){
System.out.println("File has been parsed.");
System.out.println("Continue?(y/n)");
String response = scanner.nextLine();
if (!response.equalsIgnoreCase("y")){
runProgram = false;
}
}
} catch (FileNotFoundException e){
System.out.println("File not found: "+ e.getMessage());
runProgram = false;
} catch (IOException e){
System.out.println("Error reading file: "+ e.getMessage());
runProgram = false;
}
}
System.out.println("Okay, quitting");
scanner.close();
}
private static void processLine(String line) throws BankAccountException {
String[] tokens = line.split(";");
if (tokens.length !=2){
throw new BankAccountException("Invalid Bank Account info : Missing semicolon delimiter");
}
String accountNumber = tokens[0];
String name = tokens[1];
if (accountNumber.length()!=10){
throw new BankAccountException("Invalid Bank Account info : Account number length is not 10");
}
if (!accountNumber.matches("\\d+")){
throw new BankAccountException("Invalid Bank Account info : Account number has non-digit character: "+ accountNumber);
}
if (name.length()<3||!name.matches("[a-zA-Z ]+")){
throw new BankAccountException("Invalid Bank Account info : Invalid name format");
}
System.out.println("Line "+ line +" successfully processed");
}
}
class BankAccountException extends Exception {
public BankAccountException(String message){
super(message);
}
} For this task, you'll practice writing your own exception class. Assume that you have been asked
to write a program that parses a file with bank account data. Each line of the file contains a
number, corresponding to a bank account, followed by a semicolon, followed by a person's name.
The correct format is the following:
The bank account number is a 10 digit number
The person's name is composed of ONLY alphabetic characters and/or the space, and
must be at least 3 characters long
A sample input text file, which has multiple invalid entries, is provided for you on the course
website. A portion of that file (showing two of the errors in grey) is shown in Figure 1. For this
programming task, you will write a main routine that parses the input file, line-by-line, and
throws an error (a custom error class that you'll write) if the format of the account or name for a
line in the file is invalid.
7536765490;Tatiana Harrison
984733122a;Jim

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

More Books

Students also viewed these Databases questions