【问题标题】:PrintWriter doesn't output all charactersPrintWriter 不输出所有字符
【发布时间】:2018-10-03 20:30:05
【问题描述】:
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  + "|";
}   

我的问题是我正在读取的其中一个地址的校验和为 0,并且没有将其完全输出到 labels.txt 文件。结果是这样的:

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

当我需要它变成这样时:

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

其他一切都很完美,所有其他地址都按我想要的方式输出,但只是缺少一个细节,这让我很烦。

【问题讨论】:

  • 如果邮政编码的校验和为 0,则返回来自 0 索引形式 codes 数组的值。该值由空格组成。所以输出看起来不错。如果您在问题中同时提供实际输出和预期输出,将会很有用
  • retVal += codes[chars.indexOf(returnChecksum(zipcode))]; 但是将返回值转换为char。

标签: java barcode checksum jfilechooser printwriter


【解决方案1】:

如果returnChecksum() 返回0,则codes[0] = " "。但是您想要与chars 数组中的字符“0”的索引相对应的字符串。所以像使用其他字符一样使用indexOf。这是长版本 - 你可以简化它:

// 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];

重新安排可能更容易:

String chars = "0123456789-";
String[ ] codes = {"|:|::", ":::||", "::|:|", "::||:", ":|::|",
        ":|:|:", ":||::", "|:::|", "|::|:", "||:::" };

【讨论】:

    猜你喜欢
    • 2015-03-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-11-28
    • 2021-12-21
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多