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

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:

 4289 0298 7524 0023 4289 0298 7524 0026 313 4890 444 2000 120 42 89 01 44 32 58 99 4 42 89 01 44 32 58 99 40 4289 0144 3258 9941 1234-5678-9876-5432 4289 0298 7524 002? 4289 0298 ?524 0026 4289 0298 ?524 002?

then you should output:

 VALID 313 4890 444 2000 120 42 89 01 44 32 58 99 4 4289 0298 1524 0026 4289 0298 7524 0023 4289 0298 7524 0023 INVALID 1234-5678-9876-5432 42 89 01 44 32 58 99 40 4289 0144 3258 9941 4289 0298 7524 0026 4289 0298 ?524 002?

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 validNum = new ArrayList(); ArrayList 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"); } }

}

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!