【问题标题】:Program not running when zipcode starts with 0 or ends with 0邮政编码以 0 开头或以 0 结尾时程序不运行
【发布时间】:2016-10-18 06:56:20
【问题描述】:

我的任务是为用户输入邮政编码创建一个条形码。但是,如果用户输入以 0 开头(即 05123)或校验位为 0(即 10027)的邮政编码,我无法找到正确的结果。有人有建议吗?我在一个初学者的java类。

public class Zipcode{

private String zipcode = "";
private String barcode = "";
private String tempCode = "";
private int zipnum = 0;
private int zipnumForCD = 0;
private int checkDigit = 0;
private int reversedZip = 0;
private int digitSum = 0;


 public Zipcode(int zip_number){
    zipnum = zip_number;
    zipnumForCD = zip_number;
    getCheckDigit();
    createUpdatedZip();
    createBarcode();
}

public Integer getCheckDigit(){
    while (zipnumForCD > 0){
        digitSum = digitSum + zipnumForCD % 10;
        zipnumForCD = zipnumForCD/10;
    }
    if (digitSum % 10 == 0)
    {
        checkDigit = 0;
    }
    else
    {
        checkDigit = 10 - digitSum % 10;
    }
    return checkDigit;  
}

public void createUpdatedZip(){
    zipnum = Integer.valueOf(String.valueOf(zipnum) + String.valueOf(checkDigit));
    for (int i = zipnum; i !=0; i /= 10){
        reversedZip = reversedZip * 10 + i % 10;
    }
}

public void createBarcode(){
   while (reversedZip > 0){
        switch (reversedZip % 10)
        {
            case 0: 
                tempCode = "||:::";
                break;
            case 1: 
                tempCode = ":::||";
                break;
            case 2: 
                tempCode = "::|:|";
                break;
            case 3: 
                tempCode = "::||:";
                break;
            case 4: 
                tempCode = ":|::|";
                break;
            case 5: 
                tempCode = ":|:|:";
                break;
            case 6: 
                tempCode = ":||::";
                break;
            case 7: 
                tempCode = "|:::|";
                break;
            case 8: 
                tempCode = "|::|:";
                break;
            case 9: 
                tempCode = "|:|::";
                break;
            default:
                break;
        }
        reversedZip = reversedZip / 10;
        barcode += tempCode;

        }
    barcode = "|" + barcode + "|";
}

public String getBarcode(){
   return barcode;
}

}

这是我的测试类:

import java.util.Scanner;

public class ZipTest{

public static void main(String[] args){
    Scanner input = new Scanner(System.in);

    System.out.println("Please enter a ZIP code: ");
    int zip = input.nextInt();
    Zipcode code = new Zipcode(zip);

    System.out.println("The bar code: ");
    System.out.println(code.getBarcode());
}
}

【问题讨论】:

  • 好吧,int 没有前导零,因此您可能需要重新考虑邮政编码可以用int 表示。我会切换到String
  • @KevinEsche 哦,那是真的!但是如果它不是整数,你将如何处理所有这些方法?即必须将邮政编码除以 10,等等。
  • @jaynepants 您不除以 10,而是使用 charAt() 一次提取一个数字字符。
  • 为什么有人努力回答你的问题后,你才删除它,以帮助你?
  • 你为什么要贬低这个问题的文字??

标签: java barcode zipcode


【解决方案1】:

创建一个 HashMap 实例,您将在其中存储从 '0' 到 '9' int number 的每个字符的等效条形码。

Hashmat<Character, String> charBarMap = new HashMap<Character, String>();
intBarMap.put('0',"||:::"); 
intBarMap.put('1',":::||");
...
...

由于 int 中不能有前导 0,因此您需要使用字符串格式的邮政编码。

从该字符串中检索每个字符并使用 hashMap 表获取该字符的条形码字符串表示形式('0' 到 '9')并继续附加到您的字符串。 p>

 String st = "020080";

 StringBuilder barCode = new StringBuilder();

 for(int i=0; i<st.length(); i++){
      char c = st.charAt(i);
      barCode.append(charBarMap.get(c));
 }

【讨论】:

  • 我只需将 char 值放入 key 中。
【解决方案2】:

如果变量是字符串,您可以删除前导零...然后将其转换为 int。 使用 String.replaceFirst(String regex) 删除前导零。 要转换为 int 使用类似这样的东西 int foo = Integer.parseInt("1234")

【讨论】:

  • 您知道目标是丢失前导零吗?
【解决方案3】:

您的问题属于逻辑问题:数字 1234 ... 那是什么:01234 还是 12340?!

因此,您不能将邮政编码表示为数字。相反,邮政编码的唯一有效格式是长度为 5 的 字符串,它只包含数字!

换句话说:你的类应该只接受一个字符串作为构造函数的参数;并在那里您进行进一步验证(例如检查长度/内容)并在传入的邮政编码无效时抛出异常。

因此:在任何地方都不会转换为 int;一切都发生在字符串/字符上。当然,您可以使用将索引 0、1、2 ... 处的数字作为 int 的方法;但总的来说,您最好将 ZIP 表示为字符串!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-07-20
    • 2014-12-01
    相关资源
    最近更新 更多