Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save Kauavitorio/fc90ff32993ebd31663d1bca43962045 to your computer and use it in GitHub Desktop.
Save Kauavitorio/fc90ff32993ebd31663d1bca43962045 to your computer and use it in GitHub Desktop.
Method for validating CPF in java
private static final int[] WeightCPF = {11, 10, 9, 8, 7, 6, 5, 4, 3, 2};
public static boolean isValidCPF(String cpf) {
cpf = cpf.trim().replace(".", "").replace("-", "");
if (cpf.length() != 11) return false;
for (int j = 0; j < 10; j++)
if (padLeft(Integer.toString(j), Character.forDigit(j, 10)).equals(cpf))
return false;
int Digit01 = calculateDigit(cpf.substring(0,9));
int Digit02 = calculateDigit(cpf.substring(0,9) + Digit01);
return cpf.equals(cpf.substring(0,9) + Digit01 + Digit02);
}
private static int calculateDigit(String str) {
int sum = 0;
for (int index =str.length()-1, digit; index >= 0; index-- ) {
digit = Integer.parseInt(str.substring(index,index+1));
sum += digit* WeightCPF[ WeightCPF.length-str.length()+index];
}
sum = 11 - sum % 11;
return sum > 9 ? 0 : sum;
}
private static String padLeft(String text, char character) {
return String.format("%11s", text).replace(' ', character);
}
@Kauavitorio
Copy link
Author

To call this method:

if(!isValidCPF(CPF)){
System.out.print('CPF informed is invalid')
}else{
System.out.print('CPF informed is valid')
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment