Advertisement
Guest User

Untitled

a guest
Oct 10th, 2015
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.71 KB | None | 0 0
  1. public class EANValidator {
  2. public static boolean validate(final String eanCode) {
  3. int checksum;
  4. char[] s = eanCode.toCharArray();
  5. int s1 = s[0];
  6. int s2 = s[1];
  7. int sum;
  8.  
  9. for (int i = 0; i < s.length; i++) {
  10. if (i % 2 == 0) {
  11. s1 += (s[i] * 3);
  12. } else {
  13. s2 += s[i];
  14. }
  15.  
  16. }
  17.  
  18. sum = s1 + s2;
  19.  
  20. if (sum % 10 == 0) {
  21. checksum = 0;
  22. } else {
  23. checksum = 10 - (sum % 10);
  24. }
  25.  
  26. if (checksum == s[(s.length - 1)]) {
  27. return true;
  28. } else {
  29. return false;
  30. }
  31. }
  32. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement