Created
January 11, 2016 05:26
-
-
Save cwake/d597c698174865b457dd 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
import java.math.BigInteger; | |
import java.security.InvalidAlgorithmParameterException; | |
import java.util.Scanner; | |
/** | |
* @version 1 | |
* @author ChloeWake | |
* | |
*/ | |
public class EAN13 { | |
public static void main (String[] args){ | |
String input = GetInput(); //get input from the user | |
if (input.length() < 12) { //check to see if the input is too short | |
try { | |
throw new InvalidAlgorithmParameterException("That's not a long enough barcode! Give me the rest of the numbers!"); | |
} catch (InvalidAlgorithmParameterException e) { | |
e.printStackTrace(); | |
return; | |
} | |
} | |
if (input.length() > 12 ) { //check to see if the input is too long | |
try{ | |
throw new InvalidAlgorithmParameterException("That's too long! You only need 12 numbers!"); | |
} catch (InvalidAlgorithmParameterException e){ | |
e.printStackTrace(); | |
return; | |
} | |
} | |
int ans = checkSum(input); //pass that input to the checkSum function | |
System.out.println(ans); //print out the checksum digit | |
/** | |
* | |
*/ | |
} | |
public static String GetInput(){ | |
Scanner console = new Scanner(System.in); | |
System.out.println("This program will take the first 12 numbers of a EAN13 barcode and compute the check number at the end of the code."); | |
System.out.println("Please enter a EAN13 code: "); | |
String EanCode = console.next(); | |
return EanCode; | |
} | |
public static int checkSum (String Input) { | |
int evens = 0; //initialize evens variable | |
int odds = 0; //initialize odds variable | |
int checkSum = 0; //initialize the checkSum | |
for (int i = 0; i < Input.length(); i++) { | |
//check if number is odd or even | |
if ((int)Input.charAt(i) % 2 == 0) { // check that the character at position "i" is divisible by 2 which means it's even | |
evens += (int)Input.charAt(i);// then add it to the evens | |
} else { | |
odds += (int)Input.charAt(i); // else add it to the odds | |
} | |
} | |
odds = odds * 3; //multiply odds by three | |
int total = odds + evens; //sum odds and evens | |
if (total % 10 == 0){ //if total is divisible by ten, special case | |
checkSum = 0;//checksum is zero | |
} else { //total is not divisible by ten | |
checkSum = 10 - (total % 10); //subtract the ones digit from 10 to find the checksum | |
} | |
return checkSum; | |
} | |
} | |
Here is a workable code from Apache Commons Validator:
https://gist.github.com/Rody66/3ae9898fbac690407c5e6b975fb9f15c
Use it like this: digit = EAN13CheckDigit.EAN13_CHECK_DIGIT.calculate(ean)
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I think this is one of method
private static int checkSum(String input) {
int evens = 0; //initialize evens variable
int odds = 0; //initialize odds variable
int checkSum = 0; //initialize the checkSum
for (int i=0;i<12; i++) {// fixed because it is fixed in pratices but you can use length() insted
int digit = Integer.parseInt(input.substring(i,i+1));
if (i%2==0) {
evens += digit;// then add it to the evens
}else {
odds += digit; // else add it to the odds
}
}
odds = odds * 3; //multiply odds by three
int total = odds + evens; //sum odds and evens
if (total % 10 == 0){ //if total is divisible by ten, special case
checkSum = 0;//checksum is zero
} else { //total is not divisible by ten
checkSum = 10 - (total % 10); //subtract the ones digit from 10 to find the checksum
}
return checkSum;
}