Take the 2-minute tour ×

I am trying to read in the user input of credit card number. However I got an input mistmatch after entering 10 digits. Anything below 10 digits works fine for my code.


    System.out.print("Please enter your creditcard number:");
    ccNum = scan4.nextInt();
    int length = String.valueOf(ccNum).length();
    if(length !=12)
        System.out.println("Please enter a 12 digit card number");
  }while(ccNumInfo!= true); 
share|improve this question

1 Answer 1

up vote 5 down vote accepted

This is not a problem of the scanner, it's a limitation on Java's int. Being a 32-bit number, it does not accept values beyond it's maximum value of 2147483647.

If you need to have 12 digits, use long instead. It is a 64-bit number with the upper limit of 9223372036854775807.

Using a string and validating it to contain only digits may be another acceptable solution for reading and storing card numbers: it works even for 12-digit card numbers that have leading zeros (neither long nor int would preserve leading zeros in your numbers).

share|improve this answer
Thanks for your reply. Sorry i didnt had any chance to work with long number of values to realise that. –  Wayne Apr 28 '13 at 10:30

Your Answer


