Your task is to: - Read the data listed below \"Data to Evaluate\" from a file.
ID: 3832158 • Letter: Y
Question
Your task is to: - Read the data listed below "Data to Evaluate" from a file. - Evaluate each number to see if they are representing a possible credit card number - Validate each credit card number to see if they are a valid number. - Store the valid numbers and invalid numbers in a separate array - Write the contents of validated credit card number array into a file called "valid_cards.txt" - Write the invalid credit card number array to a file called "invalid_numbcrs.txt". - Make sure to include the issuer for each group of credit card numbers identified - Your application should work with any number of credit card entries.Explanation / Answer
import java.util.Scanner;
public class CreditCardValidatorUsingLuhnAlgorithm {
public static void main(String[] args) {
Scanner scanner = new Scanner();
String str = scanner.in();
int[] numberArray = new int[str.length()];
for(int i = 0; i<str.length(); i++){
numberArray[i] = Integer.parseInt(str.substring(i, i+1));
}
for(int i = numberArray.length-2; i>=0; i=i-2){
int j = numberArray[i];
j = j*2;
if(j>9)
j = j%10 + 1;
numberArray[i]=j;
}
int sumOfAllNumbersOnCreditCard = 0;
for(int i = 0; i<numberArray.length; i++)
sumOfAllNumbersOnCreditCard += numberArray[i];
if(sumOfAllNumbersOnCreditCard%10 == 0)
System.out.println(str + "Valid Credit Card number");
else
System.out.println(str + "Invalid Credit Card number");
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.