Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

The last digit of a credit card is the check digit, which helps protect against

ID: 3649671 • Letter: T

Question

The last digit of a credit card is the check digit, which helps protect against accidentally flipping digits or mistyping a digit. You are going to write a program that takes a credit card number and determines if it is a valid number or not. Note that you are not creating a program to print money - not only is there no guarantee that you have picked a number that is in use, you don't have the expiration or the security number on the back. So, put down that Neiman Marcus catalog and get to work. Write a program called CreditCardCheck.java that takes a credit card number as a String from the user (yes, a String... int isn't going to quite be big enough here and double adds a decimal... you could use a long, but then the rest of the code gets worse in my opinion). Here's the algorithm for determining if the number is valid:

Starting from the rightmost digit (the check digit), form the sum of every other digit. For example, if the credit card number is 5490123456789128, you would sum up 8+1+8+6+4+2+0+4 = 33.
Now, for each of the remaining digits, double them, and then add together their resulting digits. Here you are left with 5 9 1 3 5 7 9 2. Doubling them gives you 10 18 2 6 10 14 18 4. Adding all the digits now gives you 1+0+1+8+2+6+1+0+1+4+1+8+4 = 37.
Add the two sums from the above steps together. 33+37=70.
If the result ends in a 0 (i.e. is evenly divided by 10), then it is a valid credit card number. Otherwise, it is not.
This method is sometimes called the "mod 10" check.
Once you have determined if the number is valid or not, you will inform the user and if the number is not valid, inform the user what the check digit should be for that card.
One hint: if you read the number as a String, you'll have to convert each character digit into the actual numeric value. Look at Integer.parseInt() as a method to make the change.


Here is an example.
0f how the prompt screen should be
Please enter a credit card number with no spaces: 5490123456789128
Valid Credit Card!

Please enter a credit card number with no spaces: 5490123456789122
Invalid Credit Card!
The check digit should be: 8

Please enter a credit card number with no spaces: 5454545454545454
Valid Credit Card!

Please enter a credit card number with no spaces: 5454545454545451
Invalid Credit Card!
The check digit should be: 4


INPUT: A String
OUTPUT:
Valid Credit Card!
--or--
Invalid Credit Card!
The check digit should be: _

please use simple FOR statements. do not use any of wrapper class objects or.. I am a beginner in programming and that is all I learned so far. Thank you

Explanation / Answer

import java.util.Scanner; class Demo { public static void main(String[] args) { String number; Scanner keyboard = new Scanner(System.in); //get the creditcard from user System.out.print("Enter the creditcard : "); number = keyboard.nextLine(); int sum1=0,sum2=0; //find the first Sum for(int i=number.length()-1;i>=0;i=i-2) { sum1 = sum1 + Character . getNumericValue( number . charAt(i)); } //find the second sum for(int i=number.length()-2;i>=0;i=i-2) { int doublenumber = 2 * Character . getNumericValue( number.charAt(i)); String doublestring = Integer.toString(doublenumber); //System.out.println(doublenumber+ " "+doublestring); for(int j=0;j
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote