How to Validate Zip Code in Java


Here this example will explain how to format and display country’s zip code in Java.

Source Code

1) ZipCode – int

package com.beginner.examples;

import java.text.DecimalFormat;

public class ZipCodeExample1 {
	public static void main(String[] args) {
		int zipCode = 43;
		DecimalFormat format = new DecimalFormat("00000");
		System.out.println(format.format(zipCode));		
	}
}

Output:

00043

2) ZipCode – String

package com.beginner.examples;

public class ZipCodeExample2 {
	public static void main(String[] args) {
				
		String zipCode = "00043";
		System.out.println(zipCode);

	}
}

Output:

00043

References

Imported packages in Java documentation:

Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments