Dont need to check with 95014 the example of 95014 given by
Dont need to check with 95014, the example of 95014 given by textbook maybe flawed. just need the general alpgorithn or approach,
Solution
import java.lang.String; public class ZipCode { private int checkDigit; private String zipNum; int zip; final String [] bars = {\":::||\", \"::|:|\", \"::|||\", \":|:::\", \":|:||\", \":||::\",\"|::::\", \"|::||\", \"|:|::\", \"||:::\"}; final String [] digits={\"1\", \"2\", \"3\", \"4\", \"5\", \"6\", \"7\", \"8\", \"9\", \"0\"}; //final String one= \" :::||\"; //final String two = \" ::|:|\"; //final String three = \" ::|||\"; //final String four = \" :|:::\"; //final String five = \" :|:||\"; //final String six = \" :||::\"; public ZipCode(String z) { this.zipNum = z; } //verify that the correct length of the zip is inputted public void checkZipCode() { if (zipNum.length() > 5 || zipNum.length() < 5) { System.out.println(\"There is an error.\"); } else { calcDigit(); } } //calculate the Check Digit public void calcDigit() { int sum = 0; int checkNum = 0; for (int i = 0; i < zipNum.length(); i++) { sum = sum + Character.digit(zipNum.charAt(i), 10); } System.out.println(sum); for (int i=0; i<10; i++) { checkNum=sum+i; if( checkNum % 10==0) { checkDigit=i; } }System.out.println(\"Check digit is \" + checkDigit); } /** * translate the zip code into a barcode * each digit represents a digit in the zip * digit1 is far right digit in the zip, with digit5 being left most digit */ public StringBuffer zipToBarCode() { StringBuffer barC = new StringBuffer(\"|\"); int total; int digit1 = 0; int digit2 = 0; int digit3 = 0; int digit4 = 0; int digit5 = 0; while ((zip % 10 ) != digit1 ) { digit1++; } barC.append(bars[digit1]); while (((zip % 100) / 10) != digit2 ) { digit2++; } barC.append(bars[digit2]); while (((zip % 1000) / 100) != digit3 ) { digit3++; } barC.append(bars[digit3]); while (((zip % 10000) / 1000) != digit4 ) { digit4++; } barC.append(bars[digit4]); while (((zip/10000)) != digit5 ) { digit5++; } barC.append(bars[digit5]); total = (zip / 10000) + ((zip % 10) / 1 ) + ((zip % 100) / 10 ) + ((zip % 1000) / 100) + ((zip % 10000) / 1000); int check = (10- (total % 10))%10; barC.append(bars[check]); barC.append(\"|\"); return barC; } //translate barcode into zip public StringBuffer barToZip() { StringBuffer bartoZip = new StringBuffer(); int num1=01; int num2=0; int num3= 0; String one= \" :::||\"; String two = \"::|:|\"; if (zipNum.contains(\"|::||\")) { num1= 1; } if (zipNum.contentEquals(\":::||\")) { num2=4; } bartoZip.append(num1+num2); return bartoZip; } }