Validar CPF

Ola pessoal,

Estou criando um método para validação de CPF, o método funciona tranquilo sem uma formatação exemplo (21729598803), adicionei a formatação e fiz um teste para retirar eles na validação mas estou encontrando um erro.

public class ValidaCPF {
	
	int soma, resultado1, resultado2;
	
	public boolean validar(String cpf) {
		
		String[] strCpf = new String[11];
		int[] numCpf = new int[11]; 
		
		for (int i = 0; i < cpf.length(); i++) {
		    
			  if ( (cpf.charAt(i) != '-') | (cpf.charAt(i) != '.')) { <--Nao funciona
		    
   		       strCpf[i] = cpf.charAt(i) + "";
		       numCpf[i]  = Integer.parseInt(strCpf[i]);
		   }
			
		}
		if ( cpf.length() == 14) {
			
			  soma = 10 * numCpf[0] + 9 * numCpf[1] + 8 * numCpf[2] + 7 * numCpf[3] + 6 * numCpf[4] + 5 * numCpf[5] + 4 * numCpf[6] + 3 * numCpf[7] + 2 * numCpf[8];
			  soma -= (11 * ((soma / 11)));
			
			  if ( soma == 0 || soma == 1) {
				resultado1 = 0;
			  }
			  else {
				resultado1 = 11 - soma;
			  }
			
			  if ( resultado1 == numCpf[9]) {
			  
			    soma = 11 * numCpf[0] + 10 * numCpf[1] + 9 * numCpf[2] + 8 * numCpf[3] + 7 * numCpf[4] + 6 * numCpf[5] + 5 * numCpf[6] + 4 * numCpf[7] + 3 * numCpf[8] + 2 * numCpf[9];
			    soma -= (11 * ((soma / 11)));
			  
			    if ( soma == 0 || soma == 1) {
				  resultado2 = 0;
			    }
			    else {
				resultado2 = 11 - soma;
			    }
				
			   }
			
		   }
		
		if ((resultado1 == numCpf[9]) && (resultado2 == numCpf[10])) {
			
			System.out.println("CPF Valido");
			return true;
		}
		
		else {
			System.out.println("CPF Invalido");
			return false;
		}
	
	}
	
	public static void main(String[] args) {
		
		ValidaCPF validar = new ValidaCPF();
		validar.validar("217.295.988-03");
		
	}
}

Como posso fazer para retirar os caracteres que nao sao números?

Obrigado

public static String SomenteNumeros(String value){
String result = “”;
for (int i=0; i<value.length();i++)
if (Character.isDigit(value.charAt(i)))
result = result + value.charAt(i);
return result;
}

ou usa este metodo Character.isDigit() no seu teste

valeu

[code]public class Other {
public static String toBy = “123.456.789-0”;
public static StringBuilder toBuild = new StringBuilder("");
public static void main(String[] args) {
String[] all = (String[]) toBy.split("[.-]");
for(String toShow : all){
toBuild.append(toShow);
}
System.out.println(toBuild);
}

}[/code]

cara…

tem uma api chamada BrazilUtils

faz de tudo:
validacao de cpf… inscricoes estaduais de todos os estados, valida cnpj, gera codigos de barra…
vale a pena conferir!!

Tava estranhando ver um post meu aqui, mas é que meio antigo, ams realmente o BrazilUtils é bem legal para validar.