# Thread: 4 digit restriction

1. Member
Join Date
Dec 2010
Posts
2
Rep Power
0

## 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

2. Senior Member
Join Date
Dec 2010
Posts
165
Rep Power
7
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)

3. Member
Join Date
Dec 2010
Posts
2
Rep Power
0
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:

Java 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);
}```

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•