//Java programming; Joyce Farrell; 8th ed //programming exercise #6 //chapter 13
ID: 3681986 • Letter: #
Question
//Java programming; Joyce Farrell; 8th ed //programming exercise #6 //chapter 13, File Input and Output 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 ValidateCheckDigitsjava. 6. . Gloname and an integerExplanation / Answer
import java.nio.file.*;
import java.io.*;
import java.nio.channels.FileChannel;
import java.nio.ByteBuffer;
import static java.nio.file.StandardOpenOption.*;
public class ValidateCheckDigits
{
public static void main(String[] args)
{
Path fileIn =
Paths.get("AcctNumsIn.txt");
Path fileOut =
Paths.get("AcctNumsOut.txt");
String acct;
int acctNum;
int lastDigit;
int digit;
int sum;
int x;
String newline = " ";
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);
lastDigit = 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 == lastDigit)
{
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);
}
}
}
AcctNumsIn.txt
345619
789400
871208
901156
984334
723422
172257
100000
273699
999995
sample output
345619 is valid
789400 is invalid
871208 is valid
901156 is valid
984334 is invalid
723422 is invalid
172257 is valid
100000 is invalid
273699 is invalid
999995 is valid
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.