Results 1 to 9 of 9
  1. #1
    nmvictor is offline Member
    Join Date
    Oct 2009
    Posts
    31
    Rep Power
    0

    Question How to validate a string in the form E25/8938/2009

    I am writing an application which requires a student to input a registration number which in our university its in the for <COURSE_CODE>/<A_UNIQUE_NO>/<YEAR_OF ENTRY_IN UNIVERSITY> e.g E35/3787/1987 OR I20/9992/1972 .I wish to validate this entry,that is
    • the first character should be alaphabetical,
    • the next two characters should be numerical,
    • the 4th and 9th charaters a "/"
    • and the rest numerical


    May someone please help me get this working, just drop me an email to victormn20@gmail.com I'll really appreciate.

  2. #2
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    8

    Default

    How about you making an attempt at it and posting if you get errors with your code?
    P.S A regex might do it.

  3. #3
    nmvictor is offline Member
    Join Date
    Oct 2009
    Posts
    31
    Rep Power
    0

    Unhappy

    I had tried with the following codes without fail I am afraid Im a new to Java and so i don't know much about regex yet, anyway check this out and i'll appreciate your suggestions

    Java Code:
    public boolean validateRegNumber(String testString)
        {      // sample regNumber E35/0847/05
            String validFirstChar = "IZECB";
            String validNum = "0123456789/";
       while (testString.length() > 11 || testString.length() <= 0){
                return false;
            }
            char c=testString.charAt(0);  //first character must be alphabetic
                if (validFirstChar.indexOf(c)== -1)
                return false;
            for (int i=testString.charAt(3); i<testString.charAt(7);i++)
            {
                char b = testString.charAt(i);
                if (validNum.indexOf(b) == -1)
                return false;
            }
        return true;
            }
    This doesnt work, i'll appreciate anyone with a more advanced and better option:(

  4. #4
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    8

    Default

    Play around with this.

    Java Code:
    System.out.print(Pattern.matches("^[A-Z]\\d{2}/\\d{4}/\\d{4}$", "E35/3787/1987"));

  5. #5
    gcampton Guest

    Default

    And in future for this kind of problem regexs are the best way to go so you really need to familiarize yourself with them, try wiki "regular expressions", "java regular expressions", and google.

    I immediately thought of regex when I was reading the question but then tried to think a way around this, Like everything in programming it can be done 101 different ways, It could be done using String.split(), String.substring(), and Scanner.hasNextInt(), switch and you can even validate year against the date class -> as years outside of 1910 - 2010 are probably invalid.
    Last edited by gcampton; 10-15-2009 at 02:13 PM.

  6. #6
    nmvictor is offline Member
    Join Date
    Oct 2009
    Posts
    31
    Rep Power
    0

    Unhappy Thanks but ...

    I tried out the suggestion you gave and it seems like what I was looking for save for a few details I havent gotten right yet, I included it in my project code as shown below
    Java Code:
    public String getRegNumber()
        {
            String regNumber;
            System.out.print("Reply with your Registration number:");
            regNumber = TextIO.getln();
            regNumber = regNumber.toUpperCase();
            boolean match;
            match = Pattern.matches( "^[A-Z]\\d{2}/\\{4}/\\d{4}$", regNumber);
            while (match != true){
                 display.displayInvalidRegNum();
                 //^^  returns a string "invalid registration number!"
                 regNumber = TextIO.getln();
                 match = Pattern.matches( "^[A-Z]\\d{2}/\\{4}/\\d{4}$", regNumber);
            }  //end while
            
            return regNumber;
    } //proceeding methods and declarations ...
    Mind not the TextIO part, its a class I am using for my input output operations.So the above looks fine to me, just a newbie in java not even a year old but im sure you'll have a different view on this.When i reply with the registration in the form E35/3038/8383 OR any similar pattern, all i get is
    Invalid Registration Number! return by Display.displayInvalidRegNum as indicated in the code above.Anyone have any further suggestions concerning this?

    @gcampton : Thanks for that suggestion, im getting down to regex,i have no choice i guess

  7. #7
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    8

    Default

    Compile the pattern once. It's the input that changes not the pattern...
    Java Code:
    public String getRegNumber() {
    		// you can use the scanner class and throw away that TextIO class
    		Scanner input = new Scanner(System.in);
    		Pattern p = Pattern.compile("^[A-Z]\\d{2}/\\d{4}/\\d{4}$");
    		String regNumber = "look Ma! An Invalid regNumber!";
    		while (!p.matcher(regNumber).find()) {
    			System.out.println("Not valid, Try again:");
    			// get more input here
    			regNumber = input.nextLine();
    		}
    		System.out.println("Valid at last");
    		return regNumber;
    	} // p

  8. #8
    nmvictor is offline Member
    Join Date
    Oct 2009
    Posts
    31
    Rep Power
    0

    Default

    lol,thanks man,That's what i was looking for,You made my day...Are you a genius in Java?Hope to get there someday,really hope!

  9. #9
    gcampton Guest

    Default

    *cry*, doesn't seem to matter how much reading I do on regex, I still don't get it... I'm tempted to buy a regex tool to do it for me though then I would have to learn how to use the tool :P

Similar Threads

  1. Need SCJP voucher in October 2009
    By shivraj_it in forum Java Certification
    Replies: 1
    Last Post: 09-20-2009, 05:57 AM
  2. Cloud Computing and Virtualization 2009 Conference
    By APTechForum in forum Forum Lobby
    Replies: 0
    Last Post: 08-17-2009, 09:17 AM
  3. Replies: 10
    Last Post: 08-12-2009, 04:02 PM
  4. Replies: 6
    Last Post: 11-28-2008, 06:55 AM
  5. Replies: 1
    Last Post: 01-11-2008, 10:06 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •