Usamos cookies para medir audiência e melhorar sua experiência. Você pode aceitar ou recusar a qualquer momento. Veja sobre o iMasters.
Algum tempo atraz eu fiz uma classe chamada CPF que serve para você trabalhar com CPFs, criando digitos verificador e checando se o CPF é valido ou não. Queria saber o que vocês acham, o que podia melhorar, se está bom =P etc.
public class CPF {
private String nCPF = "";
private String DV = "";
public CPF(String text){
setCPF(text);
}
private String FormatNum(String text){
String temp = "";
if(text.length()==11){
temp = text;
}else{
if(text.length()==9){
temp = text;
temp += this.createDV();
}else{
if(text.length()==14){
text = text.replace(".", "");
text = text.replace("-", "");
temp = text;
}
}
}
return temp;
}
public Boolean check(){
if(DV.compareToIgnoreCase(this.createDV()) != 0){
return false;
}
return true;
}
public void setCPF(String text){
nCPF = FormatNum(text).substring(0,9);
DV = FormatNum(text).substring(9,11);
}
public String getCPF(){
return nCPF+DV;
}
private String createDV(){
int temp = 0,temp2 = 0;
String DV = "";
for(int i=0;i<=9;i++){
if(i>0){
temp += (Integer.parseInt(String.valueOf(nCPF.charAt(i-1))))*i;
}
if(i<9){
temp2 += (Integer.parseInt(String.valueOf(nCPF.charAt(i))))*i;
}else{
if(temp%11 != 10){
temp = temp%11;
DV += temp;
}else{
temp = 0;
DV = "0";
}
temp2 += temp*i;
if(temp2%11 !=0){
DV += temp2%11;
}else{
DV += "0";
}
}
}
return DV;
}
}
http://forum.imasters.com.br/public/style_emoticons/default/bye1.gif
Carregando comentários...