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

QUESTION

Can someone help me to find the error?

Can someone help me to find the error? I need to read the file and recognize if the code in the file is valid. Each line will be tokenized by ";". the correct first token should be digits. my code can recognize the first line of the file and give the correct output. But when it reads the next line which has a letter "a" in the first token, it does not show anything in the output. I post the output, code and the txt file below. Thanks!

the output I got:

What file would you like to parse?

accountsFile.txt

Line7536765490;Tatiana Harrison sucessfully processed

the output I want:

What file would you like to parse.

accountsFile.txt

Line 7536765490;Tatiana Harrison successfully processed

Invalid Bank Account info : Account number has non-digit character: 984733122a

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;

              inputFile.close();

              System.exit(0);

           }

        catch(BankAccountException e){

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

        }

     }

  }

  private static boolean isValid(String accountLine)throws BankAccountException

  {

     //System.out.println(accountLine);

     boolean isValid = true;

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

     String number = strTokenizer.nextToken();

     String name = strTokenizer.nextToken();

     if(number.length() == 10&&name.length()>=3)

     {

     int i =0;

        while(isValid=true&&i<=9)

        {

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

                   isValid = true;

                   i++;

           }

           else

            {

                   isValid = false;

            }

        }

        if(isValid = true)

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

        }

        else

        {System.out.println("Invalid Bank Account Info: Account number has non-digit character: "+number);

        }

     }

     else{

     isValid = false;

     }

 return isValid;

  }

}

class BankAccountException extends Exception {

   public BankAccountException(String error) {

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

   }

}

accountsFile.txt

7536765490;Tatiana Harrison

984733122a;Jim Schwing

7462887443;John Anvik

04712f643;Razvan Andonie

9285373798;Herminia Mcshane

4398275022;Artie Baucom

2390213786;Katrice Heitkamp

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