fork download
  1. /* package whatever; // don't place package name! */
  2.  
  3. import java.util.*;
  4. import java.lang.*;
  5. import java.io.*;
  6.  
  7. /* Name of the class has to be "Main" only if the class is public. */
  8. class Ideone
  9. {
  10. public static void main(String[] args) {
  11. for (String str : Arrays.asList("046-444-286", "06-454-286", "046-454-286")) {
  12. System.out.println(str+" > "+isValidSin(str));
  13. }
  14. }
  15.  
  16. static boolean isValidSin(String sinNumber) {
  17. boolean validSin = sinNumber.matches("\\d{3}-\\d{3}-\\d{3}");
  18.  
  19. if (validSin) {
  20. String sinTwo = sinNumber.replace("-", "");
  21. int sum = 0;
  22. for (int i = 0; i < sinTwo.length(); i++) {
  23. int digit = Character.getNumericValue(sinTwo.charAt(i));
  24. if (i % 2 != 0) {
  25. digit = digit * 2;
  26. }
  27. digit = digit / 10 + digit % 10;
  28. sum += digit;
  29. }
  30. validSin = sum % 10 == 0;
  31. }
  32.  
  33. return validSin;
  34. }
  35.  
  36. }
Success #stdin #stdout 0.05s 2184192KB
stdin
Standard input is empty
stdout
046-444-286 > false
06-454-286 > false
046-454-286 > true