All pastes #960563 Raw Edit

d220698

public java v1 · immutable
#960563 ·published 2008-03-28 06:57 UTC
rendered paste body
/**  * Calculate the check digit of HKID * - Zhang Haoran, 20070912 * */import java.util.Scanner;public class ID {	// Define aliases for ASCII character code	private static final int ASC_0=48, ASC_A=65;	// Return the checkDigit produced from hkid	private static char checkDigit( String hkid ){				// Step 1: Map the leading alphabet to number: A=1, B=2, ... Z=26		// e.g. A is 65 in ASCII, so 65-65+1 gives 1		int s=((int)hkid.charAt(0)-ASC_A+1)*8;		// Step 2: Add the other digits like A*8 + 1*7 + 2*6 + ... + 6*2		for(int i=1; i<7; i++){ s += ((int)hkid.charAt(i)-ASC_0) * (8-i); }		// Step 3: s = 11 - (s mod 11)		s = 11-s%11; // ans can be 1, 2, 3, ..., 10, 11.		// Step 4: If s is 10, return A. If 11, return 0. Else return s.		s = s%11; return (s==10)?'A':(char)(s+ASC_0);	}	// Main method begins execution of Java application	public static void main( String [] args )	{		System.out.print( "Enter the first 7 digits of your HKID (e.g. A123456): " );		Scanner input = new Scanner( System.in );		String hkid = input.nextLine(); // read in a whole line, assuming correct format		System.out.printf( "Your ID number is %s(%c).\n", hkid, checkDigit(hkid));	} //end method main} // end class ID