Javaと郵便番号の例
最近、http://javanullpointer.com/java-general/528-zipcode-problem-java.html%20[leading zero problem ZipCode]に関する質問に回答しましたが、国の郵便番号の最も適切なデータタイプは何ですか?
1. ZipCode – int
Javaでは、「int」として宣言し、http://docs.oracle.com/javase/1.4.2/docs/api/java/text/DecimalFormat.html[DecimalFormat]を使用して、ゼロを導く。例えば、
package com.mkyong; import java.text.DecimalFormat; public class ZipCodeExample { public static void main(String[]args) { int zip = 123; DecimalFormat format = new DecimalFormat("00000"); System.out.println(format.format(zip)); } }
2. ZipCode – 文字列
上記の解決策は機能していますが、私は郵便番号のデータ型としてStringを使用する方がより好きです。これは、他の国で未知の郵便番号形式を使用するためにより柔軟でスケーラブルです。
package com.mkyong; public class ZipCodeExample { public static void main(String[]args) { String zip = "00123"; System.out.println(zip); } }
あなたは郵便番号に関する他の提案がありますか?
参考文献
DecimalFormatドキュメント]。
http://stackoverflow.com/questions/5130178/java-keeps-changing-the-first-zip-code
[Java
郵便番号を変更し続ける]。
http://javanullpointer.com/java-general/528-zipcode-problem-java.html
[Java
郵便番号のゼロ先問題]