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

Augment your program so that it can handle sequences in which a single digit has

ID: 3745165 • Letter: A

Question

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 if the text file contains:

then you should output:

this what I have for my code so far:

import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
import java.util.ArrayList;
import java.util.Collections;

public class CardReader {

public static String intCheck(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;
ArrayList<String> validNum = new ArrayList();
ArrayList<String> invalidNum = new ArrayList();
while (fin.hasNextLine()) {
line = fin.nextLine();
try {
modified = intCheck(line);
if(isValid(modified)) {
validNum.add(line);
} else {
invalidNum.add(line);

}
} catch (Exception e) {
invalidNum.add(line);
}
}
Collections.sort(validNum);
Collections.sort(invalidNum);
System.out.println("VALID" + validNum);
System.out.println("INVALID" + invalidNum);
fin.close();
} catch (FileNotFoundException e) {
System.out.println("Can not open " + filename + " to read");
}
}

}

Explanation / Answer

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 = 10) { digits[i] = digits[i] - 9; } } for (int i = 0; i