ok well i have to write a credit card validator
import java.util.*; // for the Scanner class
public class CCValidator
{
public static void main (String [] args)
{
Scanner sc = new Scanner(System.in);
System.out.print("Please enter a number to validate: ");
long input = sc.nextLong();
boolean isValid = numberIsValid(input);
System.out.print("This credit card number ");
if (isValid)
{
System.out.print("is ");
}
else
{
System.out.print("is NOT ");
}
System.out.println("valid.\n");
}
public static boolean numberIsValid (long num)
{
int checksum = 0;
for ( int place = 0; place < 16; place++ )
{
int digit = (int) ( num % 10 );
num /= 10;
if ( ( place & 1 ) == 0 )
{
checksum += digit;
}
else
{
checksum += ( digit );
}
if ( num == 0 )
{
break;
}
}
return ( checksum % 10 ) == 0;
}
}
these are the instructions given to us
One validation algorithm is the LUHN formula. This formula works as follows:
1. Set the sum to 0
2. Add up the digits in the credit card number, working from right to left:
* If the digit has an odd position, counting from the right, add it to the sum as-is. Thus, the first, third, etc. digits (counting from the right) are all added directly to the sum.
* Otherwise, double the value of the current digit. If it is over 10, add the digits to get a number less than 10. Then add the result to the sum.
3. If the sum divides evenly by 10 (i.e., with a remainder of 0), then the credit card number is valid. Otherwise, the card number is invalid.
For example, consider the (abbreviated and fictional) card number 499673. Doubling alternate digits (4, 9, 7), we get: (4 * 2) + 9 + (9 * 2) + 6 + (7 * 2) + 3
(9 * 2) and (7 * 2) are greater than 10, so we add the digits: (9 * 2) = 18 = (1 + 8) = 9. Doing the same for 7, we get 5. HINT: you can also just subtract 9 from the doubled digit.
The sum of these digits is 40, which divides evenly by 10, so this number is valid.
and well im terribly stuck its my 2nd maybe 3rd week of classes and afraid of falling behind if any1 can offer help or a place to look for some advice that would be wonderful