-
-
Save LuanMarcosCosta/9a0f7d524e4a3ca2bbd3a3c4e9cd66bc to your computer and use it in GitHub Desktop.
Código Java para validação do CNS (Cartão Nacional de Saúde)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| public boolean isValid(String s) { | |
| if (s.matches("[1-2]\\d{10}00[0-1]\\d") || s.matches("[7-9]\\d{14}")) { | |
| return somaPonderada(s) % 11 == 0; | |
| } | |
| return false; | |
| } | |
| private int somaPonderada(String s) { | |
| char[] cs = s.toCharArray(); | |
| int soma = 0; | |
| for (int i = 0; i < cs.length; i++) { | |
| soma += Character.digit(cs[i], 10) * (15 - i); | |
| } | |
| return soma; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment