I'm trying to calculate any ISBN-13 Number's Check Digit, I'm not too worried if the ISBN number is valid or invalid, but what I've been trying to do is get the code to work. There are obviously flaws in my interpretation of the algorithm, suggestions on how to fix it are welcome but the primary problem is receiving user input that is too large for an integer variable but I also want to avoid the decimals of the double value.
I already tried to use the BigDecimal
and BigNumber
but I simply don't have enough experience to be able to understand them completely. This is the algorithm to find d13 (the Check Digit): 10-(d1 +3d2 +d3 +3d4 +d5 +3d6 +d7 +3d8 +d9 +3d10 +d11 +3d12)%10.
The Code is a mess I know. I've used this website as a reference to what I want to do and I've been using this ISBN number as my practice: 9780132130806
.
Again my question is how can I print the final ISBN number without decimals and how can I possibly fix my algorithm? (I'd also really appreciate any tips on a website that helps with teaching JOption as that is the prefered method i use because it looks a bit cleaner to me than using the scanner)
import javax.swing.JOptionPane;
import java.math.BigInteger;
public class ISBN
{
//George Sayegh Calculate check Digit ISBN
public static void main(String[] args)
{
//Define Variables
double ISBN12, D1, D2, D3, D4, D5, D6, D7, D8, D9, D10, D11, D12 = 0, D13;
double A = 100000000000L;
double B = 10000000000L;
double C = 1000000000;
double D = 100000000;
double E = 10000000;
double F = 1000000;
double G = 100000;
double H = 10000;
double I = 1000;
double J = 100;
double K = 10;
double L = 1;
//Get ISBN #
String ISBN12text = JOptionPane.showInputDialog("Please enter the first 12 digits of your ISBN number");
ISBN12 = Double.parseDouble(ISBN12text);
//Calculate D1
D1 = ((ISBN12 - (ISBN12 % A)) / A);
//Calculate D2
D2 = ((ISBN12 - (ISBN12 % B)) / B);
//Calculate D3
D3 = ((ISBN12 - (ISBN12 % C)) / C);
//Calculate D4
D4 = ((ISBN12 - (ISBN12 % D)) / D);
//Calculate D5
D5 = ((ISBN12 - (ISBN12 % E)) / E);
//Calculate D6
D6 = ((ISBN12 - (ISBN12 % F)) / F);
//Calculate D7
D7 = ((ISBN12 - (ISBN12 % G)) / G);
//Calculate D8
D8 = ((ISBN12 - (ISBN12 % H)) / H);
//Calculate D9
D9 = ((ISBN12 - (ISBN12 % I)) / J);
//Calculate D10
D10 = ((ISBN12 - (ISBN12 % K)) / K);
//Calculate D11
D11 = ((ISBN12 - (ISBN12 % L)) / L);
//Get D13
D13 = 10 - (D1 + (3 * D2) + D3 + 3 * D4 + D5 + 3 * D6 + D7 + 3 * D8 + D9 + 3 * 10 + D11 + 3 * D12) % 10;
JOptionPane.showMessageDialog(null, D1 +""+ D2 +""+ D3 +""+ D4 +""+ D5 +""+ D6 +""+ D7 +""+ D8 +""+ D9 +""+ D10 +""+ D11 +""+ D12 +""+ 13);
}
}