-
-
Save cwake/d597c698174865b457dd to your computer and use it in GitHub Desktop.
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; | |
} | |
} | |
That code doesn't work correctly.
if ((int)Input.charAt(i) % 2 == 0) - wrong
you need to check i - index
-- fix
for (int i = 0; i < Input.length(); i++) {
*int digit = Integer.parseInt("" + Input.charAt(i));*
//check if number is odd or even
if (digit % 2 == 0) { // check that the character at position "i" is divisible by 2 which means it's even
evens += digit;// then add it to the evens
} else {
odds += digit; // else add it to the odds
}
}
-- fix
Its Wrong ! Check digit con
t use. but i don`t know where in the code, help! also thanks
As stated above, you have to check de index, not the actual number; also, as in arrays index starts at 0, you have to switch the if-else sentence:
for (int i = 0; i < Input.length(); i++) {
int digit = Integer.parseInt("" + Input.charAt(i));
if (i % 2 == 0) {
odds += digit;
} else {
evens += digit;
}
}
And for a different flavour you can get the integer value like this:
int digit = Character.getNumericValue(Input.charAt(i));
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;
}
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)
I think
odds
andevens
corresponds to digits, not the actual numbers. Thus, this is slightly wrong: https://en.wikipedia.org/wiki/International_Article_Number