Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Java problem Open AcctNumsIn.txt and look over the contents. Then open ValidateC

ID: 3837896 • Letter: J

Question

Java problem

Open AcctNumsIn.txt and look over the contents.

Then open ValidateCheckDigits.java and write a program to read in each account number from AcctNumsIn.txt and display whether it is valid.

An account number is valid only if the last digit is equal to the sum of the first five digits divided by 10. For example, the number 223355 is valid because the sum of the first five digits is 15, the remainder when 15 is divided by 10 is 5, and the last digit is 5. Write only valid account numbers to an output file, each on its own line, to a file named AcctNumsOut.txt.

Explanation / Answer

ValidateCheckDigits.java

import java.io.File;
import java.io.FileNotFoundException;
import java.io.PrintWriter;
import java.util.Scanner;


public class ValidateCheckDigits {

   public static void main(String[] args) throws FileNotFoundException {
       File inputFile = new File("D:\AcctNumsIn.txt");
       Scanner input = new Scanner(inputFile);
       File outputFile = new File("D:\AcctNumsOut.txt");
       PrintWriter pw = new PrintWriter(outputFile);
       while(input.hasNextInt()){
           int accountNum = input.nextInt();
           if(isValid(accountNum)) {
               pw.write(accountNum+" ");
           }
       }
       pw.flush();
       pw.close();
       input.close();
       System.out.println("File has been generated");
   }
   public static boolean isValid(int accountNum) {
       int lastDigit = accountNum%10;
       accountNum = accountNum/10;
       int sumOfDigits = 0;
       while(accountNum > 0) {
           int r = accountNum % 10;
           accountNum = accountNum/10;
           sumOfDigits = sumOfDigits + r;
       }
       return lastDigit == sumOfDigits% 10;
   }
}

Output:

File has been generated

AcctNumsIn.txt

223355
223366
223323
223388
223312

AcctNumsOut.txt

   223355
   223366
   223388

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote