# 4 digit restriction

• 12-27-2010, 04:04 AM
GTM
4 digit restriction
Hey guys, I'm new at the forum, hope I'm not doing nothing wrong.
I have this problem, when I tipe in the PIN (that as to be 4 digits) if the 1st digit isn't a zero it's all good, but if is not it asumes that is a shorter number (like: 0321 it assumes that the number tipped in is 321 and dosen't pass in my 4 digit restriction).
I would like to know if some one can help me with this.
thnks
GTM
• 12-27-2010, 04:38 AM
Fubarable
• 12-27-2010, 05:11 AM
JavaHater
Quote:

Originally Posted by GTM
Hey guys, I'm new at the forum, hope I'm not doing nothing wrong.
I have this problem, when I tipe in the PIN (that as to be 4 digits) if the 1st digit isn't a zero it's all good, but if is not it asumes that is a shorter number (like: 0321 it assumes that the number tipped in is 321 and dosen't pass in my 4 digit restriction).
I would like to know if some one can help me with this.
thnks
GTM

treat the input pin as a string first, check using the string length() method for 4 digits. ( i am sure you know how).
You are not doing any maths on the PIN right? If that's the case, use it as a string. There's no need to declare it as integers (or longs)
• 12-28-2010, 03:26 AM
GTM
Hey guys, I already did that, but my Input as to be a short, cuse this is a school project and they use a class to test ower classes and in that class they only input shorts as pin, so my code to validate PIN is:

Code:

```public short validarPin(String pinS) {         if (pinS.length() !=4) {             return 0;         }         for (int i = 0; i < pinS.length(); i++) {             Character a = pinS.charAt(i);             if (!a.isDigit(a)) {                 return 0;             }         }         return (short) Integer.parseInt(pinS);     }```