Henry E.

asked • 10/12/20

Create a program similar to this while the output stays the same.

Using a text editor, create a file that contains a list of at least 10 six-digit account numbers. Read in each account number 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. Save the application as ValidateCheckDigits.java



import java.nio.file.*;

import java.io.*;




public class ValidateCheckDigits {


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

Path fileIn;

fileIn = Paths.get ("C:\\Users\\Giga\\Desktop\\AcctNums.txt");

Path fileOut = Paths.get ("AcctNumsOut.txt");

String acct;

int acctNum;

int lastDigits;

int digit;

int sum;

int x;

String newLine = "\n";

InputStream input = null ;

OutputStream output = null;

try

{

input = Files.newInputStream(fileIn);

BufferedReader reader = new BufferedReader( new InputStreamReader(input));

output = Files.newOutputStream(fileOut);

acct = reader.readLine();

while(acct != null)

{

sum = 0;

acctNum = Integer.parseInt(acct);

lastDigits = acctNum % 10 ;

acctNum = acctNum /10;

for(x = 0; x < 6; x++)

{

digit = acctNum % 10;

acctNum =acctNum / 10;

sum = sum + digit;

}

sum = sum % 10 ;

if(sum == lastDigits){

System.out.println(acct + " is valid");

acct = acct + System.getProperty("line separator");

byte []data =acct.getBytes();

output.write(data);

}

else{

System.out.println(acct + "is invalid");

}

acct = reader.readLine();

}

input.close();

output.close();

}

catch (IOException e){

System.out.println(e);

}

}

1 Expert Answer

By:

Patrick B. answered • 10/12/20

Tutor
4.7 (31)

Math and computer tutor/teacher

Still looking for help? Get the right answer, fast.

Ask a question for free

Get a free answer to a quick problem.
Most questions answered within 4 hours.

OR

Find an Online Tutor Now

Choose an expert and meet online. No packages or subscriptions, pay only for the time you need.