Question
11.8 Credit Card Check Digit Banks issue credit cards with 16 digit numbers. If you've never thought about it before you may not realize it,
11.8 Credit Card Check Digit
Banks issue credit cards with 16 digit numbers. If you've never thought about it before you may not realize it, but there are specific rules for what those numbers can be. For example, the first few digits of the number tell you what kind of card it is - all Visa cards start with 4, MasterCard numbers start with 51 through 55, American Express starts with 34 or 37, etc. Automated systems can use this number to tell which company to contact without having to "look" at the card itself and see what the bank is on it.
Another key part of the credit card number standard is the check digit. The last digit of every credit card number is determined by the previous 15 digits through a simple mathematical formula known the Luhn Algorithm. The Lhun Algorithm can be used to check if a credit card number has been corrupted in its transmission between the vendor reading the card, and the bank which has the account. It can also be used to check to see if a credit card number is valid before transmitting it to the bank.
The Luhn Algorithm is described at the link above, but the basic idea is:
- From the right-to-left, double the value of each digit that is in an even-numbered position (with the check-digit at position 1). If this doubling gives you a two-digit value for any of the numbers, then subtract 9 from the value (which is easier than adding the digits together but gets you the same result). Leave the odd-valued positions as is.
- Sum together all of the values except the check digit.
- Take the digit in the one's position of the sum. If the value of that digit is 0, then it stays as 0. If the value is greater than zero, subtract that value from 10. That value should be the check digit (note that the special case for 0 is required since "10" is not a single digit).
For example, suppose the card you want to validate is: 5457623898234113. In this case the check-digit is 3 and the remaining digits are the 15-digit account number. We can confirm that we likely have a good card number by validating the check digit as follows:
Position | 16 | 15 | 14 | 13 | 12 | 11 | 10 | 9 | 8 | 7 | 6 | 5 | 4 | 3 | 2 | 1 |
Original Value | 5 | 4 | 5 | 7 | 6 | 2 | 3 | 8 | 9 | 8 | 2 | 3 | 4 | 1 | 1 | 3 |
Doubled value | 10 | 10 | 12 | 6 | 18 | 4 | 8 | 2 | ||||||||
Doubled-value adjusted | 1 | 1 | 3 | 6 | 9 | 4 | 8 | 2 | ||||||||
Sum of values = 67 | 1 | 4 | 1 | 7 | 3 | 2 | 6 | 8 | 9 | 8 | 4 | 3 | 8 | 1 | 2 | |
Check digit is 3 (10 - 7 = 3) |
You can read more about credit card numbers, as well as how the Luhn Algorithm is used in other areas of Computer Science, in this article from the Data Genetics blog (where the above example was taken from).
For this lab you will write a Java program that checks credit card strings to see if they are valid. Your program should first prompt the user to enter a string of numbers as a credit card number, or enter a blank line to quit the program. If the user doesn't quit, your program should ensure that this string is exactly 16 characters in length. If the user enters a string that is not 16 characters in length, your program should print an error message and ask again for a valid string. Your program should use the Luhn Algorithm above to compute what the check digit should be and then compare it to the actual value in the provided string and report whether the credit card number is valid or wrong. If it is wrong, your program should report what the correct check digit should be for the input value. Your program should keep asking for new values until the user enters a blank line to quit the program.
Create a new project named LuhnAlgorithm and a new Java program in that project folder named LuhnAlgorithm.java for this project. You can find a selection of valid-but-fake credit card numbers courtesy of PayPal at this link. Change the check digit on any of them to get an invalid number (note that your code should only use the 16 digit numbers and does not have to account for any of the card numbers that have any number of digits other than 16).
how to solve this problem
Enter a credit card number (enter a blank line to quit): 561651645664654346 ERROR! Number MUST have exactly 16 digits.
Enter a credit card number (enter a blank line to quit): Exception in thread "main" java.lang.IllegalStateException: Scanner closed at java.base/java.util.Scanner.ensureOpen(Scanner.java:1150) at java.base/java.util.Scanner.findWithinHorizon(Scanner.java:1781) at java.base/java.util.Scanner.nextLine(Scanner.java:1649) at LuhnAlgorithm.main(LuhnAlgorithm.java:58) and this is my code
import java.util.*; public class LuhnAlgorithm { public static void main(String[] args) { Scanner scrn = new Scanner(System.in); int a = 0 ; int b = 0; int c = 0; int d; int e; System.out.print("Enter a credit card number (enter a blank line to quit): "); String cred = scrn.nextLine(); if(cred.isEmpty()) { System.out.println("Goodbye!"); } while(cred.length()>0) { if(cred.length()!=16) { System.out.println("ERROR! Number MUST have exactly 16 digits."); } else{ a=0; b=Character.getNumericValue(cred.charAt(cred.length()-1)); cred=cred.substring(0,cred.length()-1); for(int i =0;i<15; i++) { d=Character.getNumericValue(cred.charAt(i)); if(i%2==0) { d = d*2; if(d >= 10) d-=9; } a = a + d;} e = a % 10; if(e==0) { c=0; } else { c=10-e; } System.out.println("Check digit should be: "+c); System.out.println("Check digit is: "+b); if(b==c) System.out.println("Number is valid."); else System.out.println("Number is not valid."); } System.out.print(" Enter a credit card number (enter a blank line to quit): "); cred=scrn.nextLine(); scrn.close();
} }
}
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