Verificar se RG é Válido, ajuda
09/04/2009
0
// verifica se o RG é válido em tempo de execução jRg.addFocusListener(new FocusAdapter() { String A,B,C,D,E,F,G,H,I = ""; int a,b,c,d,j,f,g,h,i,resul; public void focusLost(FocusEvent e) { A = jRg.getText().substring(0); B = jRg.getText().substring(1); C = jRg.getText().substring(3); D = jRg.getText().substring(4); E = jRg.getText().substring(5); F = jRg.getText().substring(7); G = jRg.getText().substring(8); H = jRg.getText().substring(9); I = jRg.getText().substring(11); a = Integer.parseInt(A); b = Integer.parseInt(B); c = Integer.parseInt(C); d = Integer.parseInt(D); j = Integer.parseInt(E); f = Integer.parseInt(F); g = Integer.parseInt(G); h = Integer.parseInt(H); i = Integer.parseInt(I); resul = (((2*a)+(3*b)+(4*c)+(5*d)+(6*j)+(7*f)+(8*g)+(9*h)+(10*i))/11)%10; if(resul!=0) { JOptionPane.showMessageDialog(null,"R.G. Inválido", "Informação ", JOptionPane.INFORMATION_MESSAGE); jRg.setText(""); jRg.requestFocus(); } } });
Fabio Volpe
Posts
09/04/2009
Giovane Kuhn
A = jRg.getText().substring(0, 1); B = jRg.getText().substring(1, 1); C = jRg.getText().substring(3, 1); ...
09/04/2009
Bruno Borges
09/04/2009
Bruno Borges
09/04/2009
Diego
09/04/2009
Carlos Heuberger
private static final String RG_PATTERN = "\\d\\d\\.\\d\\d\\d\\.\\d\\d\\d-\\d"; public static boolean checkRG(String rg) { // rg = "ab.cde.fgh-i" // testar formato (opcional) if (! rg.matches(RG_PATTERN)) { System.err.println(rg + " formato"); return false; } int a = rg.charAt(0) - '0'; int b = rg.charAt(1) - '0'; // . = rg.charAt(2) int c = rg.charAt(3) - '0'; int d = rg.charAt(4) - '0'; int e = rg.charAt(5) - '0'; // . = rg.charAt(6) int f = rg.charAt(7) - '0'; int g = rg.charAt(8) - '0'; int h = rg.charAt(9) - '0'; // - = rg.charAt(10) int i = rg.charAt(11) - '0'; int result = 2*a + 3*b + 4*c + 5*d + 6*e + 7*f + 8*g + 9*h + 100*i; return (result % 11) == 0; }
09/04/2009
Tiago Peczenyj
09/04/2009
Carlos Eduardo
09/04/2009
Gabriel Millian