Question

Banks issue credit cards with 16 digit numbers. If you've never thought about it before you...

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). NOTE: You do NOT need to use arrays to solve this problem - this problem can be solved just with nested loops. Solutions that use an array where it isn't needed will be penalized in two ways: First, you're making the problem much harder than it needs to be, and second there will be a point deduction for use of an unnecessary array in the solution. Sample Output: This is a sample transcript of what your program should do. Items in bold are user input and should not be put on the screen by your program. Enter a credit card number (enter a blank line to quit): 5457623898234112 Check digit should be: 3 Check digit is: 2 Number is not valid. Enter a credit card number (enter a blank line to quit): 5457623898234113 Check digit should be: 3 Check digit is: 3 Number is valid. Enter a credit card number (enter a blank line to quit): 5555555555554 ERROR! Number MUST have exactly 16 digits. Enter a credit card number (enter a blank line to quit): 5555555555554445 Check digit should be: 4 Check digit is: 5 Number is not valid. Enter a credit card number (enter a blank line to quit): Goodbye! Note: You will need to convert characters in your string into integer values to make this project work. There are two ways to do this. The first is to use Integer.parseInt and substring to get a character value. The following lines of code would give you the integer value of the first character of the string input: String s = input.substring(0,1); int val = Integer.parseInt(s); Another way to get a numeric value from a character is to use the method Character.getNumericValue. This takes a char value (not a String) and converts it to its correct integer value. The following lines of code would give the integer value of the first character of the String input:

0 0
Add a comment Improve this question Transcribed image text
Answer #1

import java.util.Scanner;

public class CreditCardValidator {
public static void main(String[] args) {
   Scanner sc = new Scanner(System.in);
   int calculatedCheck=0,check=0,sum=0,digit,last;
String creditCard;
System.out.print("Enter a credit card number (enter a blank line to quit):");
creditCard=sc.nextLine();
while(!creditCard.isEmpty()) {
   if(creditCard.length()!=16) {
       System.out.println("ERROR! Number MUST have exactly 16 digits.");
   }
   else {
       sum=0;
       check=Character.getNumericValue(creditCard.charAt(creditCard.length()-1));
   creditCard=creditCard.substring(0,creditCard.length()-1);
   for(int i=0;i<15;i++) {
           digit=Character.getNumericValue(creditCard.charAt(i));
       if (i%2==0) {
           digit=digit*2;
           if (digit>=10)
               digit-=9;
       }
       sum=sum+digit;
   }
   last=sum%10;
   if (last==0) {
       calculatedCheck=0;
   }
   else {
       calculatedCheck=10-last;
   }
   System.out.println("Check digit should be: "+calculatedCheck);
   System.out.println("Check digit is: "+check);
   if(check==calculatedCheck)
       System.out.println("Number is Valid");
   else
       System.out.println("Number is not Valid");
   }
   System.out.print("\nEnter a credit card number (enter a blank line to quit):");
creditCard=sc.nextLine();
}
   }
  
}

Add a comment
Know the answer?
Add Answer to:
Banks issue credit cards with 16 digit numbers. If you've never thought about it before you...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Credit card numbers follow certain patterns. A credit card number must have between 13 and 16...

    Credit card numbers follow certain patterns. A credit card number must have between 13 and 16 digits. The number must start with the following: 4 for Visa cards 5 for MasterCard cards 37 for American Express cards 6 for Discover cards In 1954, Hans Luhn of IBM proposed an algorithm for validating credit card numbers. The algorithm is useful to determine whether a card number is entered correctly or is scanned correctly by a scanner. Almost all credit card numbers...

  • Program Set 2 10 points) Credit card number validation Program Credit card numbers follow certain patterns:...

    Program Set 2 10 points) Credit card number validation Program Credit card numbers follow certain patterns: It must have between 13 and 16 digits, and the number must start with: 4 for Visa cards 5 for MasterCard credit cards 37 for American Express cards 6 for Discover cards In 1954, Hans Luhn of IBM proposed an algorithm for validating credit card numbers. The algorithm is useful to determine whether a card number is entered correctly or whether a credit card...

  • Validating Credit Card Numbers Write a program named Creditcard.java that prompts the user for a credit...

    Validating Credit Card Numbers Write a program named Creditcard.java that prompts the user for a credit card number and determines whether it is valid or not. (Much of this assignment is taken from exercise 6.31 in the book) Credit card numbers follow certain patterns. A credit card number must have between 13 and 16 digits, and must start with: 4 for Visa cards 5 for Master cards 6 for Discover cards 37 for American Express cards The algorithm for determining...

  • How many valid 16-digit credit card numbers are there that start with these 14 digits 4444...

    How many valid 16-digit credit card numbers are there that start with these 14 digits 4444 4444 4444 45 (according to the Luhn check)? Give one of the valid credit card numbers.

  • Consider the following credit card numbers and tell me if they are valid or not. If...

    Consider the following credit card numbers and tell me if they are valid or not. If a number is invalid, write the valid credit number by changing the “check” digit. Show your work for full marks. a. 4634 5163 4122 7983 b. 5321 4142 6379 4771 c. How many valid (according to the Luhn check) 16-digit credit card numbers are there that start with these 14 digits 4444 4444 4444 44? Give one of the valid credit card numbers.

  • Credit card numbers adhere to certain constraints. First, a valid credit card number must have between...

    Credit card numbers adhere to certain constraints. First, a valid credit card number must have between 13 and 16 digits. Second, it must start with one of a fixed number of valid prefixes : 1 • 4 for Visa • 5 for MasterCard • 37 for American Express • 6 for Discover cards In 1954, Hans Peter Luhn of IBM proposed a “checksum” algorithm for validating credit card numbers . 2 The algorithm is useful to determine whether a card...

  • The Language is for Java ------ Input ------- credit-cards-2.dat //////////////////// Output//////////// Enter a filename\n Credit card...

    The Language is for Java ------ Input ------- credit-cards-2.dat //////////////////// Output//////////// Enter a filename\n Credit card number: 3056 9309 0259 04\n Checksum: 50\n Card status: VALID\n Credit card number: 3852 0000 0232 37\n Checksum: 40\n Card status: VALID\n Credit card number: 6011 1111 1111 1117\n Checksum: 30\n Card status: VALID\n Credit card number: 6011 0009 9013 9424\n Checksum: 50\n Card status: VALID\n Credit card number: 3530 1113 3330 0000\n Checksum: 40\n Card status: VALID\n Credit card number: 3566 0020 2036...

  • 2.Consider the following credit card numbers and tell me if they are valid or not. If...

    2.Consider the following credit card numbers and tell me if they are valid or not. If a number is invalid, write the valid credit number by changing the “check” digit. Show your work for full marks. a. 4519 6731 6055 4166 b. 4515 1663 4722 3333 c. How many valid (according to the Luhn check) 16-digit credit card numbers are there that start with these 14 digits 4444 4444 4444 45? Give one of the valid credit card numbers. 3....

  • Write java program to check that a (16-digit) credit card number is valid. A valid credit...

    Write java program to check that a (16-digit) credit card number is valid. A valid credit card number will yield a result divisible by 10 when you: Form the sum of all digits. Add to that sum every second digit, starting with the second digit from the right. Then add the number of digits in the second step that are greater than four. The result should be divisible by 10. For example, consider the number 4012 8888 8888 1881. The...

  • I want it in C++ 4. When choice 4 (Verify Your Credit Card) is selected, the...

    I want it in C++ 4. When choice 4 (Verify Your Credit Card) is selected, the program should read your credit card (for example: 5278576018410787) and verify whether it is valid or not. In addition, the program must display the type (i.e. name of the company that produced the card). Each credit card must start with a specific digit (starting digit: 1st digit from left to ight), which also is used to detemine the card type according Table 1. Table...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT