Created
May 14, 2020 09:22
-
-
Save Daniiarz/338b6e60d49c25dd5be4d239411a0796 to your computer and use it in GitHub Desktop.
This file contains 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 class ISBNreader { | |
public boolean isValid(String string) { | |
String n2 = string.replaceAll("-", ""); | |
int result = 0; | |
if (n2.length() != 10) { | |
return false; | |
} | |
int counter = 10; | |
for (int i = 0; i < 10; i++) { | |
char j = n2.charAt(i); | |
try{ | |
int x = j - '0'; | |
result += counter * x; | |
} | |
catch (Exception e){ | |
if (j != 'X' && i != 9){ | |
return false; | |
} | |
result += 10; | |
} | |
counter -= 1; | |
} | |
return result % 11 == 0; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment