GST Checksum Logic in Java

GST (Goods & Service Tax) was rolled out in India w.e.f. 1st July 2017. The GST Number contains a mix of Alpha Numeric 15 digits.

Checksum is the letter mapped to the checksum number.

Here is the CHECKSUM CONVERSION TABLE.

You can find more information about Indian GST on podcasts at http://www.corporateshatak.com >> PODCAST >> GST-Blue-Saffron

www.CORPORATESHATAK.com

Below is the Java Implementation of Checksum Logic that you can copy paste & run.

package gst.in.janisoftwares;
/**
 * @author ANUP JANI
 * Checksum is the last (15th) digit of the Indian GST Number.
 * Following Java code implements logic of finding the Checksum.
 */
public class GSTChecksum {
	static final String numAlphaStr = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
	static final int lengthOfNumAlphaStr = numAlphaStr.length();
	int numAlphaStrDigitWeight, placeValueFactor, product, quotient, remainder, s, sumOfHashes, checkSumCode;
	String numAlphaStrDigit, correspondingVal, GSTN;

	/*
	 * Parameterized constructor.
	 * */
	public GSTChecksum(String GSTN) {
		this.GSTN = GSTN;
	}
	
	/*
	 * Just for visual purpose. (Like cell padding).
	 * */
	public static String indent(String dispVal) {
		String indentedVal;
		indentedVal=(String.valueOf(dispVal).length()==1?dispVal+" ":dispVal);
		return indentedVal;
	}
	
	/*
	 * Main method.
	 * */
	public static void main(String[] args) {
		String GSTN = "27AAAFJ5524C1Z";
		GSTChecksum cs = new GSTChecksum(GSTN);
		cs.runChecksum();
	}
	
	/*
	 * Business Logic
	 * */
	public void runChecksum() {
		System.out.println("----------------------------------------------");
		System.out.println("Given GST Number (14 Digits) = " + GSTN);
		System.out.println("----------------------------------------------");
		System.out.println("DIGIT WEIGHT PVF PRODUCT QUOTIENT REMAINDER");
		System.out.println("----------------------------------------------");
		for(int i = 0 ; i < GSTN.length( ) ; i++) {
			numAlphaStrDigit = String.valueOf(GSTN.charAt(i));
			numAlphaStrDigitWeight = numAlphaStr.indexOf(numAlphaStrDigit);//Corresponding Index Value.
			placeValueFactor = (i % 2 == 0 ? 1 : 2);//Alternator Place Value Factor(1 or 2)
			product = numAlphaStrDigitWeight * placeValueFactor;
			remainder = (product % lengthOfNumAlphaStr);
			quotient = product / lengthOfNumAlphaStr;
			s = quotient + remainder;
			sumOfHashes = sumOfHashes + s;
			System.out.println(indent(numAlphaStrDigit) + "     " + indent(String.valueOf(numAlphaStrDigitWeight)) + "      " + indent(String.valueOf(placeValueFactor)) + "     " + indent(String.valueOf(product)) + "       " + indent(String.valueOf(quotient)) + "        " + indent(String.valueOf(remainder)));
		}
		System.out.println("----------------------------------------------");
		System.out.println("Sum of Hashes = " + sumOfHashes);
		checkSumCode = lengthOfNumAlphaStr - (sumOfHashes % lengthOfNumAlphaStr);
		System.out.println("Checksum Code = " + checkSumCode);
		correspondingVal = String.valueOf(numAlphaStr.charAt(checkSumCode));
		System.out.println("Corresponding Value = " + correspondingVal);
		System.out.println("---------------------------------------------------");
		System.out.println("Complete GST Number (15 Digits) = " + GSTN + correspondingVal);
		System.out.println("---------------------------------------------------");
		System.out.println("www.janisoftwares.in");
		System.out.println("---------------------------------------------------");
	}
}

The output as below.

----------------------------------------------
Given GST Number (14 Digits) = 27AAAFJ5524C1Z
----------------------------------------------
DIGIT WEIGHT PVF PRODUCT QUOTIENT REMAINDER
----------------------------------------------
2      2       1      2        0         2 
7      7       2      14       0         14
A      10      1      10       0         10
A      10      2      20       0         20
A      10      1      10       0         10
F      15      2      30       0         30
J      19      1      19       0         19
5      5       2      10       0         10
5      5       1      5        0         5 
2      2       2      4        0         4 
4      4       1      4        0         4 
C      12      2      24       0         24
1      1       1      1        0         1 
Z      35      2      70       1         34
----------------------------------------------
Sum of Hashes = 188
Checksum Code = 28
Corresponding Value = S
---------------------------------------------------
Complete GST Number (15 Digits) = 27AAAFJ5524C1ZS
---------------------------------------------------
www.janisoftwares.in
---------------------------------------------------