Question
I am working on a credit card validation program and so far I got my input workign to where it will read possible valid numbers
I am working on a credit card validation program and so far I got my input workign to where it will read possible valid numbers from a file and sort them in to two array lists - one possible numbers and the second one numbers already deemed to be invalid based off the length of the number. The code for that part is below - including just a simple output method to keep track of seeing if my arraylist is working. I need help figuring out how to pass the possibleNumber array list to a Luhn formula class to compleatly validate the number. I am also going to have a class that extends the Luhn Formula class to also check what kind of card the validated number is based off the length and the first numbers of the card.
import java.io.IOException; import java.io.*; import java.util.*; import java.util.Scanner; import java.util.ArrayList; import java.io.File;
public class LuhnFormulaDemo { public static void main(String[] args) throws FileNotFoundException { String creditNumberString ; //String line = null; int posNumCount = 0; int invalNumCount= 0; ArrayList
} while (inputFile.hasNext()) { //get the poissible credit number from the file creditNumberString = inputFile.nextLine(); //find the length of the number int length = creditNumberString.length(); if (length >= 15 && length <= 19) { long creditNumberLong = Long.parseLong(creditNumberString); possibleNumber.add(creditNumberLong); posNumCount ++; } else { long creditNumberLong = Long.parseLong(creditNumberString); invalidNumber.add(creditNumberLong); invalNumCount ++; } } System.out.println("Invalind num count tests: " + invalNumCount); System.out.println("Possible valid num count tests: " + posNumCount); for(int index1 = 0; index1 < possibleNumber.size(); index1++) { System.out.println("Index # for possible: " + index1 + " Possible Credit Number: " + possibleNumber.get(index1)); } for(int index2 = 0; index2 < invalidNumber.size(); index2++) { System.out.println("Index # for invalid: " + index2 + " Invalid Credit Number: " + invalidNumber.get(index2)); } } }
Here is the basis I have at the moment for my Luhn formula import java.io.IOException; import java.io.*; import java.util.*; import java.util.Scanner; import java.util.ArrayList; import java.io.File; public class LuhnFormula { public static void Luhn() { } public ArrayList getPossibleNumbers() { } public static boolean CheckNumber(String creditNumber) { int sum = 0; boolean alternate = false; for (int i = creditNumber.length() - 1; i >= 0; i--) { int n = Integer.parseInt(creditNumber.substring(i, i + 1)); if (alternate) { n *= 2; if (n > 9) { n = (n % 10) + 1; } } sum += n; alternate = !alternate; } return (sum % 10 == 0); } }
Here are some numbers in the file im using in the first step of the validation in my main (demo) class.
3158539628375348
3337465828248404
3112804683572030
3112804683572033
5435528978467581
6706465468107999
6304362971054667
6706169762827894
6706169762827892
4844104341377697
4913405490886727
4844885754351829
4844885754351822
6371473570875275
6381475006869978
6389057917814806
347100734345549
347100734345543
6011162533805000
6011621663574413
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started