Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: Validating UK postcodes

  1. #1
    Junior Member
    Join Date
    Jul 2012
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Validating UK postcodes

    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.

     public class ValidatingPostcodes
    {
        public static boolean validateFirst4Integers( String First4Integers )
        {
            return First4Integers.matches( "[a-z]")
        }
    }

    Its not much but I dont know what to do.. HELP!


  2. #2
    Member
    Join Date
    Jun 2012
    Location
    Left Coast, USA
    Posts
    451
    My Mood
    Mellow
    Thanks
    1
    Thanked 97 Times in 88 Posts

    Default Re: Validating UK postcodes

    Quote Originally Posted by Mehwish-S View Post
    ...Its not much ...
    Well, it's a start.

    I perceive that you have already recognized that you need a regular expression.

    I would make three Strings since it's easier to test one thing at a time.

    I would probably call the Strings "firstPart," "separator," and "isecondPart."

    Then the regular expression String used in the String.matches() statement in the final program would simply be firstPart + separator + isecondPart.


    OK, let's get started...

    I would (probably) do the easy part first:
    The "separator" is a single space, right? (Wow! We are one third of the way through already!)

    Then, I would try to express the other parts in Human Language in a form that can be translated to Regular Expression lingo as spelled out in the Java Regular Expression Tutorial


    From your description, I think the Strings would be defined something like the following:

    • firstPart:

      One or two Letters followed by
      One or two Digits followed by
      Zero or one Letter


    • separator:

      One Space


    • secondPart:

      One Digit followed by
      Two Letters


    Where
    Letter: [A-Z]
    Digit: [0-9]
    Space: [ ]


    In the tutorial, here's where it tells you how you handle repeated fields: Regular Expression Quantifiers

    Now if my description turns out not to be completely accurate in terms of accepting valid codes and/or rejecting invalid codes (I'm a Yank, you know; English is just another Foreign Language to me), then you might be able to use my thoughts as a starting point and refine things if your testing turns up problems.

    Bottom line:
    Test, test, and test some more. Give lots of valid codes, but don't just give it Good Stuff; try to find Bad Stuff that is "almost-but-not-quite" Good.


    Cheers!

    Z
    Last edited by Zaphod_b; July 28th, 2012 at 03:29 PM.

Similar Threads

  1. validating java text field
    By payal in forum AWT / Java Swing
    Replies: 1
    Last Post: July 24th, 2012, 12:41 AM
  2. need help with validating results
    By tim1234 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: December 13th, 2011, 10:50 AM
  3. Validating fields in a js shopping cart
    By rockc in forum What's Wrong With My Code?
    Replies: 1
    Last Post: February 10th, 2010, 03:37 PM
  4. Replies: 1
    Last Post: February 28th, 2009, 10:05 PM

Tags for this Thread