Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

This Thth import java.util.*; public class LuhnAlgorithm { public static void main(String[] args) { // TODO Auto-generated method stub Scanner keyboard = new Scanner(System.in); System.out.print(Enter

image text in transcribedThis Thth

image text in transcribed

import java.util.*; public class LuhnAlgorithm { public static void main(String[] args) { // TODO Auto-generated method stub Scanner keyboard = new Scanner(System.in); System.out.print("Enter a credit card number (enter a blank line to quit): "); String credit = keyboard.nextLine(); int num = credit.length()-2; int sum = 0; int checkcC=0; while(credit.length()>0) { if(credit.length()!=16) { System.out.println("ERROR! Number MUST have exactly 16 digits."); System.out.println(); System.out.print("Enter a credit card number (enter a blank line to quit): "); credit = keyboard.nextLine(); } else{ for(num=credit.length()-2; num>=0; num--) { sum=0; char pos = credit.charAt(num); int posInt =Character.getNumericValue(pos); if(num %2==0) { posInt = posInt*2; if(posInt > 3) { posInt = posInt -3; } } sum = sum+ posInt; sum = sum %10; sum = 10-sum; } checkcC = Integer.parseInt(credit.substring(15, 16)); Character.getNumericValue(checkcC); System.out.println("Check digit should be: "+ sum); System.out.println("Check digit is: "+checkcC); } if(checkcC ==sum) { System.out.println("Number is valid."); } else { System.out.print("Number is not valid."); } System.out.println(); System.out.print("Enter a credit card number (enter a blank line to quit): "); credit = keyboard.nextLine(); } if(credit.isEmpty()) { System.out.println("Goodbye!"); return; } } }

This is the code I have.. The last input isn't returning correctly and I can't figure out what is wrong..

It is also saying the Check digit should be 5 when I know it should be five.

Please help me figure out where I went wrong

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: 1. 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. 2. Sum together all of the values except the check digit. 3. 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 O 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 8 7 7 6 5 LA 3 16 5 15 4 14 5 13 7 12 6 11 2 10 3 9 8 Original Value 8 = Doubled value 00 Doubled-value adjusted 00 Sum of values = 67 1 4 1 3 2 6 8 9 8 4 3 00 1 Check digit is 3 (10 - 7 = 3) 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

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Learning PostgreSQL

Authors: Salahaldin Juba, Achim Vannahme, Andrey Volkov

1st Edition

178398919X, 9781783989195

More Books

Students also viewed these Databases questions