user2973473
user2973473

Reputation: 1

Credit card validation

I need help to convert from a string to int, i've searched and didnt find anything that can help me with this specific question.

this is what i got so far.. (code down)

Now after combining the first 5 digits (e.g 1+2+3+4+5=12345) i need to MOD this number in 7 (12345%7)

but it keeps giving me the next error -

java.lang.number.formatexception for input string: fiveDigits:(in java.lang.number.formatexception)

Any guidelines on how to fix it will be much appreciated. Thanks.

if(creditCard<=99999||creditCard>=1000000)
         System.out.println("Your credit card is not valid. You cannot buy the ticket");
         else
        {

                ones = creditCard%10;
                tens = (creditCard%100)/10;
                hundreds = (creditCard%1000)/100;
                thousands = (creditCard%10000)/1000;
                tensOfThousands = (creditCard%100000)/10000;
                hunOfThousands = (creditCard%1000000)/100000;

                String fiveDigits = "" + hunOfThousands+tensOfThousands+thousands+hundreds+tens;
                int firstFiveDigits = Integer.parseInt("fiveDigits");
                int remainder = firstFiveDigits%7;


                if(remainder==ones)
                 System.out.println("Your credit card is valid. Bon Voyage!");
                else
                   System.out.println("Your credit card is not valid. You cannot buy the ticket");  

Upvotes: 0

Views: 253

Answers (2)

arkonautom
arkonautom

Reputation: 958

int firstDigits = (allDigits - allDigits%10)/10;

It ain't good but it's better ...

You can operate with "real" CC numbers as strings (as they are quite long) - just get your allDigitsAsString as input and then

String firstDigitsAsString = allDigitsAsString.substring(0, allDigitsAsString.length() - 1);
int firstDigits = Integer.parseInt(firstDigitsAsString);

For bonus points add some imitation call to a payment processor stub.

Upvotes: 0

Marc Ridders
Marc Ridders

Reputation: 351

Remove the quotes surrounding the variable fiveDigits

int firstFiveDigits = Integer.parseInt(fiveDigits);

Upvotes: 2

Related Questions