Advertisement
ksoltan

JM Chapter 10 #20

Mar 11th, 2015
270
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.17 KB | None | 0 0
  1. An ISBN has thirteen digits. The first twelve identify the country in which the book was printed, the publisher, and the individual book. The thirteenth digit is the "check digit". It is chosen in such a way that (d1 + 3d2 + d3 + 3d4 ... + d11 + 3d12 + d13) mod 10 = 0
  2. where d1, d2, d3.. are digits of the ISBN. mod stands for modulo division ( same as % in Java)
  3.  
  4. Look in JM Chapter 10 #20
  5.  
  6. "9781604596908", true
  7. "8580001040332", true
  8. "9781477550601", true
  9. "9780861129720", true
  10. "9780486115085", true
  11. "9780439574280", true
  12. "9781581180671", true
  13. "9781306593625", true
  14. "9781444740349", true
  15. "9781494715410", true
  16. "9781484022184", true
  17. "9781500581046", true
  18. "9780195852578", true
  19. "9781453751763", true
  20. "9785935568931", true
  21. "9781245617412", false
  22. "1246189461829", false
  23. "9714128451124", false
  24. "9238642383519", false
  25. "9384629571557", false
  26. "2348762948653", false
  27.  
  28. public boolean isValidISBN(String isbn){
  29. int sumDigits = 0;
  30. for(int i = 0; i < isbn.length(); i++){
  31. if(i % 2 == 1){
  32. sumDigits += 3 * Character.digit(isbn.charAt(i), 10);
  33. }
  34. else{
  35. sumDigits += Character.digit(isbn.charAt(i), 10);
  36. }
  37. }
  38. return sumDigits % 10 == 0;
  39. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement