Use Jakarta-ORO for ISBN validation. The Jakarta-ORO Java classes are a set of text-processing Java classes that provide Perl5 compatible regular expressions, AWK-like regular expressions, glob expressions, and utility classes for performing substitutions, splits, filtering filenames, etc.
Download Jakarta-ORO jar
import org.apache.oro.text.perl.Perl5Util; public class ISBNValidator { private static final String SEP = "(\\-|\\s)"; private static final String GROUP = "(\\d{1,5})"; private static final String PUBLISHER = "(\\d{1,7})"; private static final String TITLE = "(\\d{1,6})"; private static final String CHECK_ISBN10 = "([0-9X])"; private static final String CHECK_ISBN13 = "(\\d)"; private static final String PREFIX = "(978|979)"; private static final String ISBN10_PATTERN = "/^" + GROUP + SEP + PUBLISHER + SEP + TITLE + SEP + CHECK_ISBN10 + "$/"; private static final String ISBN13_PATTERN = "/^" + PREFIX + SEP + GROUP + SEP + PUBLISHER + SEP + TITLE + SEP + CHECK_ISBN13 + "$/"; private static final String EAN13_PATTERN = "/^" + PREFIX + "\\d{10}$/"; public ISBNValidator() { super(); } public boolean isValid(String isbn) { return isValidISBN10(isbn) || isValidISBN13(isbn); } public boolean isValidISBN10(String isbn) { if (isbn == null || isbn.length() < 10 || isbn.length() > 13) { return false; } if (isFormatted(isbn) && !isValidPatternISBN10(isbn)) { return false; } isbn = cleanISBN10(isbn); if (isbn.length() != 10) { return false; } return (sumISBN10(isbn) % 11) == 0; } public boolean isValidISBN13(String isbn) { if (isbn == null || isbn.length() < 13 || isbn.length() > 17) { return false; } if (isFormatted(isbn)) { if (!isValidPatternISBN13(isbn)) { return false; } isbn = toEAN13(isbn); } return isValidEAN13(isbn); } public boolean isValidEAN13(String ean13) { if (ean13 == null || ean13.length() != 13 || !isValidPatternEAN13(ean13)) { return false; } return (sumEAN13(ean13) % 10) == 0; } private int sumISBN10(String isbn) { int total = 0; for (int i = 0; i < 9; i++) { int weight = 10 - i; total += (weight * toInt(isbn.charAt(i))); } total += toInt(isbn.charAt(9)); // add check digit return total; } private int sumEAN13(String isbn) { int total = isbn.charAt(0); for (int i = 1; i < 13;) { total += (3 * isbn.charAt(i++)) + isbn.charAt(i++); } return total; } private String cleanISBN10(String isbn) { StringBuffer buf = new StringBuffer(10); for (int i = 0; i < isbn.length(); i++) { char digit = isbn.charAt(i); if (Character.isDigit(digit) || (digit == 'X')) { buf.append(digit); } } return buf.toString(); } private String toEAN13(String isbn) { StringBuffer buf = new StringBuffer(13); for (int i = 0; i < isbn.length(); i++) { char digit = isbn.charAt(i); if (Character.isDigit(digit)) { buf.append(digit); } } return buf.toString(); } private int toInt(char ch) { return (ch == 'X') ? 10 : Character.getNumericValue(ch); } private boolean isFormatted(String isbn) { return ((isbn.indexOf('-') != -1) || (isbn.indexOf(' ') != -1)); } private boolean isValidPatternISBN10(String isbn) { return new Perl5Util().match(ISBN10_PATTERN, isbn); } private boolean isValidPatternISBN13(String isbn) { return new Perl5Util().match(ISBN13_PATTERN, isbn); } private boolean isValidPatternEAN13(String isbn) { return new Perl5Util().match(EAN13_PATTERN, isbn); } }
ISBN Validation:
ReplyDeleteProblem:
An ISBN number is legal if it consists of 10 digits and
d_1 + 2*d_2 + 3*d_3 + … + 10*d_10 is a multiple of 11.
The ISBN number 0-201-31452-5 is valid.
1*5 + 2*2 + 3*5 + 4*4 + 5*1 + 6*3 + 7*1 + 8*0 + 9*2 + 10*0 = 88
and 88 is a multiple of 11.
Write a program which validates whether an ISBN number.
For Ex:
1) if user inputs 8535902775 then it should print “Valid”
2) if user inputs 1843369283 then it should print “Not Vali
Solution:
import java.util.*;
public class test {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
String read = sc.nextLine();
if (read.length() == 10)
{
int sum=0,count=0,z=0;
for (int i=10;i>=2 ;i–)
{
String readnum = read.substring(count,count+1);
count++;
int number = Integer.parseInt(readnum);
sum+= i*number;
z=11-(sum%11);
}
String chk = read.substring(9,10);
int chknum = Integer.parseInt(chk);
if (chknum == z)
{ System.out.println(“Valid”);
}
else{ System.out.println(“Not Valid”);
}
}
else{ System.out.println(“Input digits = 10 digits!!!”);
}
}
}