Skip to content

Instantly share code, notes, and snippets.

@Daniiarz
Created May 14, 2020 09:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Daniiarz/338b6e60d49c25dd5be4d239411a0796 to your computer and use it in GitHub Desktop.
Save Daniiarz/338b6e60d49c25dd5be4d239411a0796 to your computer and use it in GitHub Desktop.
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