Programa de validação de CPF

Boa tarde pessoal,

Preciso criar um programa que pede o seu CPF, ele tem que ter 11 caracteres, até aqui tudo bem, porém pede que seja somente números aqui é o problema.

Tenho conhecimento para criar uma condição para verificar se os caracteres são diferentes de 0,1,2,3,4,5,6,7,8,9, mas acredito que tenha um método mais fácil de fazer essa comparação.

Vocês conhecem?

Artur Martini

Se o ambiente for web você pode usar o Stella

[quote=ErickMacedo]Se o ambiente for web você pode usar o Stella
http://blog.caelum.com.br/caelum-stella-o-cinto-de-utilidades-para-o-desenvolvedor-brasileiro/[/quote]

Não é mas obrigado pela atenção.

Mais alguém pode ajudar?

Boa tarde vc esta fazendo com interface ??

Pode usar a api da Stella da caelum.

No construtor vc pode informar que a string vem formatada ou não.( CPFValidator(boolean isFormatted) )

CPFValidator vld = new CPFValidator();

Simples

Vc importa o Jar é pode usar em qualquer projeto web ou desktop.

import javax.swing.JOptionPane;

public class Exercicio0802 {
	public static void main(String[] args) {
		
		do {
			String cpf = "";
			boolean verificador = false;
			
			String st = "Informe seu CPF";
			cpf = JOptionPane.showInputDialog(st);
			
			if (cpf == null){
				System.exit(0);
			}
			
			if (cpf.equals("")){
				st = "Digite o número do cpf!";
				JOptionPane.showMessageDialog(null, st,"Erro",0);
				continue;
			}
			
			if (cpf.length()!= 11){
				st = "O cpf deve ter 11 caracteres!";
				JOptionPane.showMessageDialog(null, st,"Erro",0);
				continue;
			}
			
			if (cpf.charAt(0) == '0' || cpf.charAt(0) == '1' || cpf.charAt(0) == '2' || cpf.charAt(0) == '3' || cpf.charAt(0) == '4' ||
					cpf.charAt(0) == '5' || cpf.charAt(0) == '6' || cpf.charAt(0) == '7' || cpf.charAt(0) == '8' || cpf.charAt(0) == '9'){
				
			}else {
				st = "CPF deve conter apenas números";
				JOptionPane.showMessageDialog(null, st,"Erro",0);
				continue;
			}
			if (cpf.charAt(1) == '0' || cpf.charAt(1) == '1' || cpf.charAt(1) == '2' || cpf.charAt(1) == '3' || cpf.charAt(1) == '4' ||
					cpf.charAt(1) == '5' || cpf.charAt(1) == '6' || cpf.charAt(1) == '7' || cpf.charAt(1) == '8' || cpf.charAt(1) == '9'){
				
			}else {
				st = "CPF deve conter apenas números";
				JOptionPane.showMessageDialog(null, st,"Erro",0);
				continue;
			}
			if (cpf.charAt(2) == '0' || cpf.charAt(2) == '1' || cpf.charAt(2) == '2' || cpf.charAt(2) == '3' || cpf.charAt(2) == '4' ||
					cpf.charAt(2) == '5' || cpf.charAt(2) == '6' || cpf.charAt(2) == '7' || cpf.charAt(2) == '8' || cpf.charAt(2) == '9'){
				
			}else {
				st = "CPF deve conter apenas números";
				JOptionPane.showMessageDialog(null, st,"Erro",0);
				continue;
			}
			if (cpf.charAt(3) == '0' || cpf.charAt(3) == '1' || cpf.charAt(3) == '2' || cpf.charAt(3) == '3' || cpf.charAt(3) == '4' ||
					cpf.charAt(3) == '5' || cpf.charAt(3) == '6' || cpf.charAt(3) == '7' || cpf.charAt(3) == '8' || cpf.charAt(3) == '9'){
				
			}else {
				st = "CPF deve conter apenas números";
				JOptionPane.showMessageDialog(null, st,"Erro",0);
				continue;
			}
			if (cpf.charAt(4) == '0' || cpf.charAt(4) == '1' || cpf.charAt(4) == '2' || cpf.charAt(4) == '3' || cpf.charAt(4) == '4' ||
					cpf.charAt(4) == '5' || cpf.charAt(4) == '6' || cpf.charAt(4) == '7' || cpf.charAt(4) == '8' || cpf.charAt(4) == '9'){
				
			}else {
				st = "CPF deve conter apenas números";
				JOptionPane.showMessageDialog(null, st,"Erro",0);
				continue;
			}
			if (cpf.charAt(5) == '0' || cpf.charAt(5) == '1' || cpf.charAt(5) == '2' || cpf.charAt(5) == '3' || cpf.charAt(5) == '4' ||
					cpf.charAt(5) == '5' || cpf.charAt(5) == '6' || cpf.charAt(5) == '7' || cpf.charAt(5) == '8' || cpf.charAt(5) == '9'){
				
			}else {
				st = "CPF deve conter apenas números";
				JOptionPane.showMessageDialog(null, st,"Erro",0);
				continue;
			}
			if (cpf.charAt(6) == '0' || cpf.charAt(6) == '1' || cpf.charAt(6) == '2' || cpf.charAt(6) == '3' || cpf.charAt(6) == '4' ||
					cpf.charAt(6) == '5' || cpf.charAt(6) == '6' || cpf.charAt(6) == '7' || cpf.charAt(6) == '8' || cpf.charAt(6) == '9'){
				
			}else {
				st = "CPF deve conter apenas números";
				JOptionPane.showMessageDialog(null, st,"Erro",0);
				continue;
			}
			if (cpf.charAt(7) == '0' || cpf.charAt(7) == '1' || cpf.charAt(7) == '2' || cpf.charAt(7) == '3' || cpf.charAt(7) == '4' ||
					cpf.charAt(7) == '5' || cpf.charAt(7) == '6' || cpf.charAt(7) == '7' || cpf.charAt(7) == '8' || cpf.charAt(7) == '9'){
				
			}else {
				st = "CPF deve conter apenas números";
				JOptionPane.showMessageDialog(null, st,"Erro",0);
				continue;
			}
			if (cpf.charAt(8) == '0' || cpf.charAt(8) == '1' || cpf.charAt(8) == '2' || cpf.charAt(8) == '3' || cpf.charAt(8) == '4' ||
					cpf.charAt(8) == '5' || cpf.charAt(8) == '6' || cpf.charAt(8) == '7' || cpf.charAt(8) == '8' || cpf.charAt(8) == '9'){
				
			}else {
				st = "CPF deve conter apenas números";
				JOptionPane.showMessageDialog(null, st,"Erro",0);
				continue;
			}
			if (cpf.charAt(9) == '0' || cpf.charAt(9) == '1' || cpf.charAt(9) == '2' || cpf.charAt(9) == '3' || cpf.charAt(9) == '4' ||
					cpf.charAt(9) == '5' || cpf.charAt(9) == '6' || cpf.charAt(9) == '7' || cpf.charAt(9) == '8' || cpf.charAt(9) == '9'){
				
			}else {
				st = "CPF deve conter apenas números";
				JOptionPane.showMessageDialog(null, st,"Erro",0);
				continue;
			}
			if (cpf.charAt(10) == '0' || cpf.charAt(10) == '1' || cpf.charAt(10) == '2' || cpf.charAt(10) == '3' || cpf.charAt(10) == '4' ||
					cpf.charAt(10) == '5' || cpf.charAt(10) == '6' || cpf.charAt(10) == '7' || cpf.charAt(10) == '8' || cpf.charAt(10) == '9'){
				
			}else {
				st = "CPF deve conter apenas números";
				JOptionPane.showMessageDialog(null, st,"Erro",0);
				continue;
			}
				
			
				st = "CPF válido: " + cpf;
				JOptionPane.showMessageDialog(null, st,"Mensagem",1);
				verificador = true;
				System.exit(0);
			
			} while (true);
		}

}

O programa está rodando perfeitamente.

Minha dúvida é a seguinte: alguém sabe uma maneira “mais simples” de fazer essa verificação se todos os caracteres são números?

Cara o paulo.rqo cabou de falar que você pode usar o Stella para desktop

Isso resolve seu problema

http://javaaberto.blogspot.com.br/2012/11/validar-cpf.html

Amigo, como já citado acima, podes utilizar o Stella da Caelum. Outra coisa, é incluir a validação dos caracteres repetidos tipo: 111.111.111-11;

Outra coisa que você perguntou sobre como não deixar passar números, como seu sistema não é Web, caso seja Swing tem um componente de Mascara para ele, onde você joga um pattern tipo: “999.999.999-99”. Isso define que o usuário só poderá digitar números e já vai formatado.