5 Replies - 954 Views - Last Post: 18 July 2012 - 11:30 AM Rate Topic: -----

#1 MSheraz  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 07-April 12

Write a Java program to validate postcode input.

Posted 18 July 2012 - 11:00 AM

In general, the UK postcode format is one of "A9 9AA", "A99 9AA", "AA9 9AA", "AA99 9AA", "A9A 9AA" or "AA9A 9AA", where A is an alphabetic character and 9 is a numeric character.

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 :/ Help Please!

Is This A Good Question/Topic? 0
  • +

Replies To: Write a Java program to validate postcode input.

#2 CasiOo  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 991
  • Posts: 2,200
  • Joined: 05-April 11

Re: Write a Java program to validate postcode input.

Posted 18 July 2012 - 11:05 AM

First of all you are told to use String.matches(String regex) not a Scanner.

You can use \d for a digit and \w for a word character.
Was This Post Helpful? 0
  • +
  • -

#3 Martyr2  Icon User is offline

  • Programming Theoretician
  • member icon

Reputation: 3872
  • View blog
  • Posts: 11,405
  • Joined: 18-April 07

Re: Write a Java program to validate postcode input.

Posted 18 July 2012 - 11:08 AM

Re-read the instructions. It told you to use the matches() method from the string class to determine if the pattern matches. Read in the string, give it to a string variable. Use that string variable to execute matches() and test it against each pattern.


http://docs.oracle.c...va.lang.String)

http://www.tutorials...ing_matches.htm
:)
Was This Post Helpful? 0
  • +
  • -

#4 MSheraz  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 07-April 12

Re: Write a Java program to validate postcode input.

Posted 18 July 2012 - 11:12 AM

Stupid me lol
But i have never used String.matches().. so any suggestions?
and thanks :)

Stupid me lol
But i have never used String.matches().. so any suggestions?
and thanks :)
Was This Post Helpful? 0
  • +
  • -

#5 CasiOo  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 991
  • Posts: 2,200
  • Joined: 05-April 11

Re: Write a Java program to validate postcode input.

Posted 18 July 2012 - 11:27 AM

View PostMSheraz, on 18 July 2012 - 06:12 PM, said:

Stupid me lol
But i have never used String.matches().. so any suggestions?
and thanks :)

Stupid me lol
But i have never used String.matches().. so any suggestions?
and thanks :)


What exactly do you need help with? Look the method up in the java SE API if you are unsure what the method looks like :)

This can get you started I hope
"BD7 1DP".matches("regex1");
"BD7 1DP".matches("regex2");
"BD7 1DP".matches("regex3");
"BD7 1DP".matches("regex4");


Was This Post Helpful? 0
  • +
  • -

#6 MSheraz  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 07-April 12

Re: Write a Java program to validate postcode input.

Posted 18 July 2012 - 11:30 AM

It fine I have got it. Thank for the help :)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1