0
public static void main(String[] args) throws FileNotFoundException {

    // Open file containing names with FileChooser dialog
    JFileChooser chooser = new JFileChooser( );
    chooser.showOpenDialog(null);
    File fileObj = chooser.getSelectedFile( );

    // Read names and write greetings, each in their own file.
    Scanner in = new Scanner(fileObj);
    PrintWriter pw;
    pw = new PrintWriter(new File("labels.txt"));
    while (in.hasNextLine( )) {
        String line = in.nextLine();
        String[ ] fields = line.split(",");
        String name = fields[0];
        String address = fields[1];
        String city = fields[2];
        String state = fields[3];
        String zipcode = fields[4];

        pw.println(name);
        pw.println(address);
        pw.println(city + ", " + state + " " + zipcode);
        pw.println(getBarCode(zipcode));
        pw.println();
    }
    in.close( );
    pw.close( );
}

public static int returnChecksum(String zipcode) {

    int sumOfDigits = 0;
    int value = 0;
    int checksum = 0;

    for(int i = 0; i < zipcode.length(); i++) {
        if (zipcode.charAt(i) != '-'){
            value = Integer.parseInt(
                    zipcode.substring(i,  (i + 1)));
            sumOfDigits += value;
        }
    }
    checksum = (10 - sumOfDigits % 10) % 10;
    return checksum;
}

public static String getBarCode(String zipcode)  {
    String chars = " 1234567890-";
    String[ ] codes = {"   ", ":::||", "::|:|", "::||:", ":|::|",
            ":|:|:", ":||::", "|:::|", "|::|:", "|:|::", "||:::" , ""};
    String retVal = "";

    for(int i = 0; i < zipcode.length( ); i++) {
        char c = zipcode.charAt(i);
        int index = chars.indexOf(c);
        String code = codes[index];
        retVal += code ;
    }
    retVal += codes[returnChecksum(zipcode)];

    return "|" + retVal  + "|";
}   

My problem is that one of the addresses I'm reading turns out a checksum of 0 and does not output it to the labels.txt file completely. It turns out like:

Meredith Baker
1343 Maple Avenue
Denver, CO 80236-2982
||::|:||:::::|:|::||::||::::|:||:|::|::|:::|:|   |

When I need it to turn out like this:

Meredith Baker
1343 Maple Avenue
Denver, CO 80236-2982
||::|:||:::::|:|::||::||::::|:||:|::|::|:::|:|||:::|

Everything else is perfect, all of the other addresses output just like how I want it, but it's just that one detail that's missing and it's bugging me.

  • 5
    If checksum is 0 for zipcode, then value from 0 index form `codes` array is returned. That value consists of spaces. So output looks OK. It would be useful if you provide both actual and expected outputs in the question – Ivan Oct 03 '18 at 20:35
  • 1
    `retVal += codes[chars.indexOf(returnChecksum(zipcode))];` But converting the returned value to char. – 001 Oct 03 '18 at 20:40

1 Answers1

0

If returnChecksum() returns 0, then codes[0] = " ". But you want the string that corresponds to the index of the char '0' in the chars array. So use indexOf as you did with the other chars. Here's the long version - you can simplify it:

// 0 <= checksum <= 9
int checksum = returnChecksum(zipcode);
// Convert single digit number to char
char checksumAsChar = Integer.toString(checksum).charAt(0);
// Find in string. 1 <= index <= 10
int index = chars.indexOf(checksumAsChar);
retVal += codes[index];

It might just be easier to re-arrange:

String chars = "0123456789-";
String[ ] codes = {"|:|::", ":::||", "::|:|", "::||:", ":|::|",
        ":|:|:", ":||::", "|:::|", "|::|:", "||:::" };
001
  • 13,291
  • 5
  • 35
  • 66