Question: Augment your program so that it can handle sequences in which a single digit has been corrupted . That is, the character '?' may appear

Augment your program so that it can handle sequences in which a single digit has been corrupted. That is, the character '?' may appear in a sequence in the place of an unknown digit. Because of the check number, it should be possible to determine the value of the missing digit. Augment your program so that it determines the missing digit and adds the corresponding sequence to the list of valid sequences. Note: any code that contains more than one '?' is considered invalid.

for example:

sequence: 7992739871?

luhns algorithm applied: 7994769772?

sum of all digits:67+?

multiply by 9 = 603 + ?

check digit is right most digit of product: ? = 3

this isn't the only way to determine the check digit, but it's the way that is used in the code, if you need more information on luhns algorithm it can be found on : https://en.wikipedia.org/wiki/Luhn_algorithm

1.From the rightmost digit, which is the check digit, and moving left, double the value of every second digit. The check digit is not doubled; the first digit doubled is immediately to the left of the check digit. If the result of this doubling operation is greater than 9 (e.g., 8 2 = 16), then add the digits of the product (e.g., 16: 1 + 6 = 7, 18: 1 + 8 = 9) or, alternatively, the same result can be found by subtracting 9 from the product (e.g., 16: 16 9 = 7, 18: 18 9 = 9).

2.Take the sum of all the digits.

2.If the total modulo 10 is equal to 0 (if the total ends in zero) then the number is valid according to the Luhn formula; else it is not valid.

more info can be found here: https://en.wikipedia.org/wiki/Luhn_algorithm

what I have so far:

import java.io.File; import java.io.FileNotFoundException; import java.util.Scanner; public class ValidateCreditCardNumbers { public static String ignoreDashAndWhitespaces(String str) throws Exception { String result = ""; for(int i = 0; i < str.length(); ++i) { if(Character.isDigit(str.charAt(i))) { result += str.charAt(i); } else if(!Character.isWhitespace(str.charAt(i))) { throw new Exception("Invalid character found"); } } return result; } public static boolean isValid(String str) { char checkDigit = str.charAt(str.length() - 1); String digit = calculateCheckDigit(str.substring(0, str.length() - 1)); return checkDigit == digit.charAt(0); } public static String calculateCheckDigit(String str) { String digit; int sum = 0; int[] digits = new int[str.length()]; for (int i = 0; i < str.length(); i++) { digits[i] = Character.getNumericValue(str.charAt(i)); } for (int i = digits.length - 1; i >= 0; i -= 2) { digits[i] += digits[i]; if (digits[i] >= 10) { digits[i] = digits[i] - 9; } } for (int i = 0; i < digits.length; i++) { sum += digits[i]; } sum = sum * 9; digit = sum + ""; return digit.substring(digit.length() - 1); } public static void main(String[] args) { Scanner in = new Scanner(System.in); System.out.print("Enter file name: "); String filename = in.nextLine(); try { Scanner fin = new Scanner(new File(filename)); String line, modified; while (fin.hasNextLine()) { line = fin.nextLine(); try { modified = ignoreDashAndWhitespaces(line); if(isValid(modified)) { System.out.println(line + " VALID"); } else { System.out.println(line + " INVALID"); } } catch (Exception e) { System.out.println(line + " INVALID"); } } fin.close(); } catch (FileNotFoundException e) { System.out.println("Can not open " + filename + " to read"); } } }

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!