Write a Java program to validate postcode input for each of these formats.
Hints:
Convert each format into a regular expression;
Write a Java class (test-bed prototype) with these regular expressions as constant strings;
Use the method String.matches() to check if the input string matches the first regular expression, in which case print a confirmation message, e.g.
if ("BD7".matches("[A-Z]{2}[0-9]")) System.out.println("valid postcode");
Add code to validate and confirm matches to each of the formats.
How many of your regular expressions are matched by the postcode "BD7 1DP" ?
package validatingpostcodes;
import java.util.Scanner;
public class ValidatingPostcodes {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Please enter a Postcode:");
while (!sc.hasNext("[A-Z]{2}[0-9]" )) {
System.out.println("Invalid!");
sc.next();
}
String Postcode = sc.next();
System.out.println("Valid Postcode");
}}
Thats how far I have got to and now im totally lost

New Topic/Question
Reply



MultiQuote






|