ISBN-10 is a standard for identifying books. It uses 10 digits d?dzd3dadsdsdzdsd
ID: 3703129 • Letter: I
Question
ISBN-10 is a standard for identifying books. It uses 10 digits d?dzd3dadsdsdzdsdsd?e. The last digit d?e is a checksum which is calculated from the other digits using the following formula: If the checksum is 10, the last digit is denoted as X according to the ISBN-10 convention. Write a Java class, ISBN, that prompts the user to enter an ISBN-10 number as a string, and then reports if the string is valid by verifying the checksum. Your program should read the input as a string, and then use a loop to compute the checksum, and verify that the 10th digit matches with the computed checksum. Here are some sample runs: Enter an ISBN-10 number: 0136012671 This is a valid ISBN-10 numberExplanation / Answer
ISBN.java
import java.util.Scanner;
public class ISBN {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
System.out.println("Enter an ISBN-10 number: ");
String s = scan.next();
int checkSum = 0;
for(int i=0;i<s.length()-1;i++) {
checkSum=checkSum+ (i+1)*Integer.parseInt(s.charAt(i)+"");
}
checkSum=checkSum%11;
int lastDigit = Integer.parseInt(s.charAt(s.length()-1)+"");
if(lastDigit==checkSum) {
System.out.println("This is a valid ISBN-10 number");
} else {
System.out.println("This is NOT a valid ISBN-10 number");
}
}
}
Output:
Enter an ISBN-10 number:
0136012671
This is a valid ISBN-10 number
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.