#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2013
    Posts
    2
    Rep Power
    0

    Credit card validation


    hi,

    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)

    The card has to be 6 digit long (e.g 123456) - got it
    I've isolated each digit of the card number (ones, tens, hundreds...)
    I've combined the first five digits (from the left) of the card using a string.
    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: fiveDigitsin 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");
  2. #2
  3. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2013
    Posts
    6
    Rep Power
    0
    Try to use

    int firstFiveDigits = Integer.parseInt(fiveDigits);

    instead of

    int firstFiveDigits = Integer.parseInt("fiveDigits");
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2013
    Posts
    2
    Rep Power
    0
    Gentelman, you live up to your name. Thanks you !

IMN logo majestic logo threadwatch logo seochat tools logo