Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 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
- where d1, d2, d3.. are digits of the ISBN. mod stands for modulo division ( same as % in Java)
- Look in JM Chapter 10 #20
- "9781604596908", true
- "8580001040332", true
- "9781477550601", true
- "9780861129720", true
- "9780486115085", true
- "9780439574280", true
- "9781581180671", true
- "9781306593625", true
- "9781444740349", true
- "9781494715410", true
- "9781484022184", true
- "9781500581046", true
- "9780195852578", true
- "9781453751763", true
- "9785935568931", true
- "9781245617412", false
- "1246189461829", false
- "9714128451124", false
- "9238642383519", false
- "9384629571557", false
- "2348762948653", false
- public boolean isValidISBN(String isbn){
- int sumDigits = 0;
- for(int i = 0; i < isbn.length(); i++){
- if(i % 2 == 1){
- sumDigits += 3 * Character.digit(isbn.charAt(i), 10);
- }
- else{
- sumDigits += Character.digit(isbn.charAt(i), 10);
- }
- }
- return sumDigits % 10 == 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement