Question: I still have a problem (checking the IIN Range - the red square on the image I post below of the credit card ) on
I still have a problem (checking the IIN Range - the red square on the image I post below of the credit card ) on my assignment.. Can someone help me to fix it? Thank you very much!
THIS IS MY CODE:
import java.io.BufferedWriter; import java.io.File; import java.io.FileNotFoundException; import java.io.FileWriter; import java.io.IOException; import java.util.ArrayList; import java.util.Scanner; public class CreditCardValidate { public static void main(String args[]) {
//array lists for storing card data //using this as not sure how many entities tobe read ArrayList input_cards = new ArrayList(); ArrayList valid_cards = new ArrayList(); ArrayList invalid_cards = new ArrayList();
// reading and storing input file data into arraylist Scanner scanner; try { //reading data from input.txt file scanner = new Scanner(new File("input.txt")); while (scanner.hasNextLine()) { input_cards.add(scanner.nextLine().trim()); } } catch (FileNotFoundException e) { e.printStackTrace(); }
// validating and separating each card based on identity
for (String cardNumber : input_cards) { if (validateCreditCardNumber(cardNumber)) { valid_cards.add(cardNumber); }
else invalid_cards.add(cardNumber); }
// writing valid and invalid cards into separate files writeToFile("valid_cards.txt", valid_cards,true); //true means card type must include in file writeToFile("invalid_cards.txt", invalid_cards,false); //false means no need of card type names in file System.out.println("done..."); }
//method to get card type based on card number private static String getCreditCardType(String cardNumber) { String cType = null;
if (cardNumber.startsWith("4026") || cardNumber.startsWith("417500")|| cardNumber.startsWith("4508")|| cardNumber.startsWith("4844")|| cardNumber.startsWith("4913")|| cardNumber.startsWith("4917") && cardNumber.length()==16 ) { cType = "VISA Electron"; }
else if (cardNumber.startsWith("51") || cardNumber.startsWith("53")|| cardNumber.startsWith("52")|| cardNumber.startsWith("54")|| cardNumber.startsWith("55") && (cardNumber.length()>=16 && cardNumber.length()
else if (cardNumber.startsWith("37") || (cardNumber >= "622126" && cardNumber
else if (cardNumber.startsWith("6011")|| cardNumber.startsWith("644")|| cardNumber.startsWith("645")|| cardNumber.startsWith("646")|| cardNumber.startsWith("647")|| cardNumber.startsWith("648")|| cardNumber.startsWith("649")|| cardNumber.startsWith("65") && cardNumber.length()==16) { cType = "Discover"; }
else if (cardNumber >= "3528" && cardNumber
else if (cardNumber.startsWith("54") && cardNumber.length()==16) { cType = "Diners Club - USA & Canada "; }
else if (cardNumber.startsWith("300")|| cardNumber.startsWith("301")|| cardNumber.startsWith("302")|| cardNumber.startsWith("303")|| cardNumber.startsWith("304")|| cardNumber.startsWith("305") && cardNumber.length()==14) { cType = "Diners Club-Carte Blanche"; }
else if (cardNumber.startsWith("36") && cardNumber.length()==14) { cType = "Diners Club-International"; }
else if (cardNumber.startsWith("5018")|| cardNumber.startsWith("5020")|| cardNumber.startsWith("5038")|| cardNumber.startsWith("5893")|| cardNumber.startsWith("6304")|| cardNumber.startsWith("6759")|| cardNumber.startsWith("6761")|| cardNumber.startsWith("6762")|| cardNumber.startsWith("6763") && (cardNumber.length()>=16 && cardNumber.length()
else if (cardNumber.startsWith("6304")|| cardNumber.startsWith("6706")|| cardNumber.startsWith("6771")|| cardNumber.startsWith("6709") && (cardNumber.length()>=16 && cardNumber.length()
else if (cardNumber.startsWith("4") && (cardNumber.length()>=13 && cardNumber.length()
else if (cardNumber.startsWith("637")|| cardNumber.startsWith("638")|| cardNumber.startsWith("639") && cardNumber.length()==16) { cType = "InstaPayment"; } else { cType = "UnknownType"; }
return cType; }
// function to validate given card number is valid private static boolean validateCreditCardNumber(String str) {
int[] ints = new int[str.length()]; for (int i = 0; i = 0; i = i - 2) { int j = ints[i]; j = j * 2; if (j > 9) { j = j % 10 + 1; } ints[i] = j; } int sum = 0; for (int i = 0; i
// Write into File -> Data of string array to write static void writeToFile(String filename, ArrayList cards, boolean flag) { BufferedWriter output = null; try { File file = new File(filename); // Create file object output = new BufferedWriter(new FileWriter(file));
for (String cardNumber : cards) { // writing data); output.write(" " + cardNumber + "\t");
if (flag) //flag to check is it necessary to include card type output.write(getCreditCardType(cardNumber)+" ");
}
} catch (IOException e) {// Handling exceptions e.printStackTrace(); } finally { if (output != null) { try { output.close(); } catch (IOException e) { e.printStackTrace(); } } } System.out.println("data written successfully into "+filename); } }


ow to validate Credit Card Numbers Most credit card number can be validated using the Luhn algorithm, which is more or a less a glorified Modulo 10 formula! The Luhn Formula: Drop the last digit from the number. The last digit is what we want to check against Reverse the numbers . Multiply the digits in odd positions (1, 3, 5, etc.) by 2 and subtract 9 to all any result higher than 9 .Add all the numbers together .The check digit (the last number of the card) is the amount that you would need to add to get a multiple of 10 (Modulo 10) Luhn Example: Steps Total Original Number: Drop the last digit Reverse the digits Multiple odd digits by 2: Add digits from line above Add all numbers Mod 10: 4 5 5 6 7 37 5 8 6 899 8 5 10 8 18 916 6 16 5 14 3 14 6 1058 1 8 9 9 7 6 7 5 53 5 6 5 8 (85 +X) modulo 10-0, thus X-5 (last digit of card) 85 List of credit card number formats Credit Card Issuer Starts With (IIN Range) Length (Number of digits) 34, 37 300, 301, 302, 303, 304, 305 36 54 6011 622126 to 622925 644, 645, 646 647 637, 638, 639 3528 to 3589 6304, 6706, 6771, 6709 5018, 5020, 5038, 5893, 6304, 6759, 6761, 6762, 6763 51, 52, 53, 54, 55 4 4026, 417500, 4508, 4844,4913, 4917 16 American Express Diners Club- Carte Blanche Diners Club International Diners Club-USA &Canada 14 14 16 16 16 16 16-19 Discover InstaPayment JCB Maestro MasterCard Visa Visa Electron 16-19 13-16
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
