Answered You can hire a professional tutor to get the answer.

QUESTION

Why my catch block in the main method does not work when I find error? How can I correct?

Why my catch block in the main method does not work when I find error? How can I correct? The try catch block should be in the main method. The isValid() method should be invoked in the try block. The header of the isValid()method cannot be changed( private static boolean isValid(String accountLine)throws BankAccountException). Thanks!

Output should be:

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

or

What file would you like to parse.

accountsFile.txt

Line 7536765490;Tatiana Harrison successfully processed

Line 7462887443;John Anvik successfully proc

essed

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.

import java.io.*;

import java.util.Scanner;

import java.util.StringTokenizer;

public class BankAccountProcessor{

  public static void main(String args[])throws FileNotFoundException{

     String fileName;

     boolean runProgram = true;

     System.out.println("What file would you like to parse?");

     Scanner keyboard = new Scanner(System.in);

     fileName = keyboard.nextLine();

     File file = new File(fileName);

     Scanner inputFile = new Scanner(file);

      while(runProgram == true){

        try{

           while(inputFile.hasNext()){

              String content = inputFile.nextLine();

              isValid(content);

                          }

              runProgram = false;

           }

        catch(BankAccountException e){

           System.out.println("Do you want to continue or quit?(y or n)");

           Scanner keyboardd = new Scanner(System.in);

          char answer = keyboardd.next().charAt(0);

          if(answer == 'y')

          { runProgram = true;

          }

          else

          {

              System.out.println("quitting");

              System.exit(0);     

          }

        }

     }

      inputFile.close();

  }

  private static boolean isValid(String accountLine)throws BankAccountException

  {

     boolean valid = true;

     StringTokenizer strTokenizer = new StringTokenizer(accountLine,";");

     String number = strTokenizer.nextToken();

     String name = strTokenizer.nextToken();

        int i =0;

        for(i=0;i<=9;i++){

        if (!Character.isDigit(number.charAt(i)))

        {valid = false;}

           }

           if (valid ==true){

       System.out.println("Line"+accountLine+" sucessfully processed");

                    }

  return valid;

  }

}

class BankAccountException extends Exception {

   public BankAccountException(String error) {

       super("Invalid Bank Account info : " + error);

   }

}

accountsFile.txt

7536765490;Tatiana Harrison

7462887443;John Anvik

984733122a;Jim Schwing

04712f643;Razvan Andonie

9285373798;Herminia Mcshane

4398275022;Artie Baucom

Show more
LEARN MORE EFFECTIVELY AND GET BETTER GRADES!
Ask a Question