Results 1 to 4 of 4
  1. #1
    bobbyboyy is offline Member
    Join Date
    Oct 2009
    Location
    Palm Springs
    Posts
    3
    Rep Power
    0

    Smile String Tokenizer - Basic email validation

    Hey i'm new to Java and i'm currently following some tutorials. I've recently been looking at String Tokenizer.

    What I want to try now is to use string tokenizer to validate an email address. I've looked around and see there is a way of doing it with reg expressions but for now I want to do this with the tokenizer.

    I think I have coded the tokenizer correctly. But how could I go about verifying the e-mail?

    I want to check that there is exactly 1 @ symbol, following the @ symbol, there is at least 1 . character and that there are no characters other than letters, digits, and @ . – and _

    Here is my tokenizer code so far...

    Java Code:
        public static boolean validEmail(String address) {
            StringTokenizer emailTokens;
            String[] tokens;
            int noOfTokens, count;
    
            emailTokens = new StringTokenizer(address, "@");
            noOfTokens = emailTokens.countTokens();
            count = 0;
            tokens = new String[noOfTokens];
    
            while (emailTokens.hasMoreTokens()) {
            tokens[count] = emailTokens.nextToken();
            count++;
            }
    How would I now use this code to check that an email is valid? :o

    Any help is very much appreciated! Thankyou i've found this forum very useful :)

  2. #2
    mrmatt1111's Avatar
    mrmatt1111 is offline Senior Member
    Join Date
    Aug 2009
    Location
    San Jose, CA, USA
    Posts
    320
    Rep Power
    5

    Default

    Instead of String tokenizer i would look at String.split(regex) instead.
    My Hobby Project: LegacyClone

  3. #3
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    I'm thinking that to validate an expression you wouldn't use a String tokenizer or String.split, but the rather the basic regular expression (regex) library including the Matcher class. Note that I'm no expert in this, far from this.

  4. #4
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    19

    Default

    Quote Originally Posted by Fubarable View Post
    I'm thinking that to validate an expression you wouldn't use a String tokenizer or String.split, but the rather the basic regular expression (regex) library including the Matcher class. Note that I'm no expert in this, far from this.
    Yes, this is the most easiest, most suitable way. I don't think string tokernize should merge with this. It's simple as this,

    Java Code:
          // e-mail pattern string, any characters before the @ sign and letters after that
          Pattern p = Pattern.compile(".+@.+\\.[a-z]+");
    
          // Match the given string(e-mail) with the pattern
          Matcher m = p.matcher(your_emial_address_as_a_String);
    
          // Is found?
          boolean b= m.matches();

Similar Threads

  1. Live Email Validation In Java
    By shameel in forum Web Frameworks
    Replies: 9
    Last Post: 01-05-2010, 05:42 AM
  2. Manipulating String Tokenizer
    By Bomber_Will in forum New To Java
    Replies: 2
    Last Post: 04-19-2009, 11:54 PM
  3. string tokenizer
    By twinytwo in forum New To Java
    Replies: 2
    Last Post: 03-26-2009, 02:10 PM
  4. Problem with string tokenizer
    By twinytwo in forum AWT / Swing
    Replies: 2
    Last Post: 03-26-2009, 11:27 AM
  5. question on string tokenizer
    By munigantipraveen in forum New To Java
    Replies: 2
    Last Post: 05-23-2008, 05:00 AM

Posting Permissions

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