Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Using a text editor, submit a file that contains a list of at least 10 six-digit account numbers. Read in each account number and display

Using a text editor, submit 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. Write only valiu d account numbers in an output file, each on its own line. Save the application as 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) { Scanner fileScanner = null; PrintWriter fileWriter = null;

String inputFileName ="accountNumbers.txt"; String outputFileName ="validaccounts.txt";

try { fileScanner = new Scanner(new File(inputFileName)); fileWriter = new PrintWriter(new File(outputFileName)); while(fileScanner.hasNextLine()) { int account = Integer.parseInt(fileScanner.nextLine()); if(isValidAccount(account)) { fileWriter.println(account); } } } catch (FileNotFoundException e) { System.out.println("Exception opening file"); } fileScanner.close(); fileWriter.close(); } private static boolean isValidAccount(int account) { int lastDigit = account %10; int temp = account/10; int sum = 0; while(temp!=0) { sum += temp%10; temp = temp/10; } if(sum%10 == lastDigit) return true; else return false; } }

----jGRASP exec: java ValidateCheckDigits Exception opening file Exception in thread "main" java.lang.NullPointerException: Cannot invoke "java.io.PrintWriter.close()" because "fileWriter" is null at ValidateCheckDigits.main(ValidateCheckDigits.java:34)

----jGRASP wedge2: exit code for process is 1. ----jGRASP: operation complete. validaccounts.txt

223355

555555

123456

542153

124333

656545

876786

345365

769679

556455

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Introduction to Wireless and Mobile Systems

Authors: Dharma P. Agrawal, Qing An Zeng

4th edition

1305087135, 978-1305087132, 9781305259621, 1305259629, 9781305537910 , 978-130508713

More Books

Students also viewed these Programming questions