Email Validation

Without using pattern or regex.I can only use if and String functions.

  • (2 Pages)
  • +
  • 1
  • 2

27 Replies - 10014 Views - Last Post: 11 March 2010 - 09:48 PM Rate Topic: -----

#1 goonjverma  Icon User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 39
  • Joined: 21-February 10

Email Validation

Posted 09 March 2010 - 12:41 AM

Write a program to evaluate an email.
Check if the String representing the email address actually “looks like”
an email address; to simplify, we can assume that an email contains one
and only one @ character and contains one or more periods after the @
character.If it does “look like” an email address, then the method return
true; otherwise, it returns false.

I have written a bit... dont know if its right or wrong->
if(email.charAt(i).equals("@"))
count++;
if(email.charAt(i)=='@')
count++;


Is This A Good Question/Topic? 0
  • +

Replies To: Email Validation

#2 japanir  Icon User is offline

  • jaVanir
  • member icon

Reputation: 1010
  • View blog
  • Posts: 3,025
  • Joined: 20-August 09

Re: Email Validation

Posted 09 March 2010 - 12:49 AM

if(email.charAt(i).equals("@")) 

this line is wrong, email.charAt(i) will retun a char value.
you cannot compare a char value to a String Object.
remember:
"@" is a String Object
'@' is a char.

Quote

we can assume that an email contains one
and only one @ character

if you can assume that, why do you have to check it in the method?

does your assignment specifies any default pattern for an email?
Was This Post Helpful? 0
  • +
  • -

#3 n8wxs  Icon User is offline

  • --... ...-- -.. . -. ---.. .-- -..- ...
  • member icon

Reputation: 972
  • View blog
  • Posts: 3,878
  • Joined: 07-January 08

Re: Email Validation

Posted 09 March 2010 - 12:49 AM

You want indexOf in Class String.

...
String email = "fred@dream.in.code.net"
...
if (email.indexOf('@') < 0) {
    System.out.println("No '@' in email address");
}
...


Was This Post Helpful? 0
  • +
  • -

#4 goonjverma  Icon User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 39
  • Joined: 21-February 10

Re: Email Validation

Posted 09 March 2010 - 01:08 AM

Sorry I can not use pattern method as we have not done it in class.
Was This Post Helpful? 0
  • +
  • -

#5 zim1985  Icon User is offline

  • Grand Inquisitor
  • member icon

Reputation: 74
  • View blog
  • Posts: 568
  • Joined: 19-February 10

Re: Email Validation

Posted 09 March 2010 - 01:10 AM

View Postgoonjverma, on 08 March 2010 - 11:08 PM, said:

Sorry I can not use pattern method as we have not done it in class.

That's not a Pattern method. It's part of String...
Was This Post Helpful? 0
  • +
  • -

#6 goonjverma  Icon User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 39
  • Joined: 21-February 10

Re: Email Validation

Posted 09 March 2010 - 01:22 AM

one of my friend sent this code to me but i have not learned anything like public static boolean isEmail(String email)
Can you tell me what should i use instead of specifying boolean like that


public static boolean isEmail(String email){
if(email.indexOf("@")!=-1){
if( email.substring(email.indexOf("@")).inde… && email.substring(email.indexOf("@")+1).in…
return true;
}
return false;
}


This post has been edited by goonjverma: 09 March 2010 - 01:23 AM

Was This Post Helpful? 0
  • +
  • -

#7 japanir  Icon User is offline

  • jaVanir
  • member icon

Reputation: 1010
  • View blog
  • Posts: 3,025
  • Joined: 20-August 09

Re: Email Validation

Posted 09 March 2010 - 01:40 AM

your assignment instructions were:

Quote

If it does “look like” an email address, then the method return true; otherwise, it returns false.

what the code you posted describes is a method which returns a boolean value (true\false). it is declared static, i guess because you call it inside the main method which is static.
if you don't want to use a method, just have those if statements and conditions. and instead of the return statements, use print statements.
like:
public static void main(String[] args){

String email;
//get input for email

//add more checks to to valid the email as needed.
if(email.indexOf("@")!=-1){
     System.out.println("a valid email");
}
else {
    System.out.println("not a valid email");
}
}

Was This Post Helpful? 0
  • +
  • -

#8 goonjverma  Icon User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 39
  • Joined: 21-February 10

Re: Email Validation

Posted 09 March 2010 - 11:33 AM

but i have to use boolean with those if-else statements.
Was This Post Helpful? 0
  • +
  • -

#9 japanir  Icon User is offline

  • jaVanir
  • member icon

Reputation: 1010
  • View blog
  • Posts: 3,025
  • Joined: 20-August 09

Re: Email Validation

Posted 09 March 2010 - 12:23 PM

so just have a boolean variable, that gets true if the email is valid, or false if not:
public static void main(String[] args){ 
 
String email; 
//get input for email 

//if the email is valid
boolean isValid = false;

//add more checks to to valid the email as needed. 
if(email.indexOf("@")!=-1){
     isValid = true; 
} 

if(isValid){
      System.out.println("a valid email");
}
else{
    System.out.println("not a valid email"); 
}

}

Was This Post Helpful? 1
  • +
  • -

#10 goonjverma  Icon User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 39
  • Joined: 21-February 10

Re: Email Validation

Posted 09 March 2010 - 12:41 PM

Thank you :)

This post has been edited by goonjverma: 09 March 2010 - 12:42 PM

Was This Post Helpful? 0
  • +
  • -

#11 goonjverma  Icon User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 39
  • Joined: 21-February 10

Re: Email Validation

Posted 09 March 2010 - 02:24 PM

I need to fix this program.I want the user to enter email address until it is valid, but I'm not getting how to do it.

/*
 * To write a program to evaluate an email address
 */

//Use the import statement for input
import java.util.Scanner;

//Defining class EmailValidation
public class EmailValidation {

    //Deifning the main method
  public static void main(String[] args)
  {
//Declare a scanner variable names input and initilaize it
//using a new scanner object created using System.in

Scanner input= new Scanner(System.in);

//Declare string object email
String email;


int count=0;
int countP=0;


//Prompt user to enter input for email
System.out.println("Enter the email address");
email = input.next();

   //Using for loop to check leangth of email address entered by the user
for(int i = 0; i<email.length();i++)
   {

    //check if email contains @ or not
    if(email.charAt(i)=='@')
        count++;
   }
   


       //chck for count value
       if (count==1)
       {

          //check for email address
          int index = email.indexOf('@');


          String temp = email.substring(index);

             for(int i = 0; i<temp.length();i++)
                {

                   if(temp.charAt(i)=='.')
                   countP++;

                }

             if(countP>0)
             {
              //Display that email address is valid
              System.out.println("Valid");
              }
             else
             {
              //Display that email is not valid
              System.out.println("Invalid");
             }
       }
       else
            //Prompt user to enter the email address again if it is inavlid
            System.out.println("Enter a valid email address again");
            email = input.next();
          
          
  //End of class
  }
//End of main method
}




Was This Post Helpful? 0
  • +
  • -

#12 zim1985  Icon User is offline

  • Grand Inquisitor
  • member icon

Reputation: 74
  • View blog
  • Posts: 568
  • Joined: 19-February 10

Re: Email Validation

Posted 09 March 2010 - 05:06 PM

View Postgoonjverma, on 09 March 2010 - 12:24 PM, said:

I need to fix this program.I want the user to enter email address until it is valid, but I'm not getting how to do it.

/*
 * To write a program to evaluate an email address
 */

//Use the import statement for input
import java.util.Scanner;

//Defining class EmailValidation
public class EmailValidation {

    //Deifning the main method
  public static void main(String[] args)
  {
//Declare a scanner variable names input and initilaize it
//using a new scanner object created using System.in

Scanner input= new Scanner(System.in);

//Declare string object email
String email;


int count=0;
int countP=0;


//Prompt user to enter input for email
System.out.println("Enter the email address");
email = input.next();

   //Using for loop to check leangth of email address entered by the user
for(int i = 0; i<email.length();i++)
   {

    //check if email contains @ or not
    if(email.charAt(i)=='@')
        count++;
   }
   


       //chck for count value
       if (count==1)
       {

          //check for email address
          int index = email.indexOf('@');


          String temp = email.substring(index);

             for(int i = 0; i<temp.length();i++)
                {

                   if(temp.charAt(i)=='.')
                   countP++;

                }

             if(countP>0)
             {
              //Display that email address is valid
              System.out.println("Valid");
              }
             else
             {
              //Display that email is not valid
              System.out.println("Invalid");
             }
       }
       else
            //Prompt user to enter the email address again if it is inavlid
            System.out.println("Enter a valid email address again");
            email = input.next();
          
          
  //End of class
  }
//End of main method
}




Instead of using if-else statements, why not just loop it in a do-while block?

String emailIn = "";
boolean works = true;
Scanner input = new Scanner(System.in);

do
{
    try
    {
        System.out.print("Enter email address: ");
        emailIn = input.getNextLine();
        works = true;
    }
    catch(InputMismatchException e)
    {
        System.out.println("Please enter a valid email address!");
    }
} while(works);


This post has been edited by zim1985: 09 March 2010 - 05:07 PM

Was This Post Helpful? -2
  • +
  • -

#13 goonjverma  Icon User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 39
  • Joined: 21-February 10

Re: Email Validation

Posted 09 March 2010 - 07:45 PM

i have to use if else...and i cnt use catch...
Was This Post Helpful? 0
  • +
  • -

#14 Dogstopper  Icon User is offline

  • The Ninjaducky
  • member icon



Reputation: 2870
  • View blog
  • Posts: 11,021
  • Joined: 15-July 08

Re: Email Validation

Posted 09 March 2010 - 07:58 PM

zim, an InputMismatchException is NOT going to determine whether the address is valid...

What I suggest doing is using the String.indexOf() method to
A) Determine if there is a "@" character
B) The text after the "@" contains a period
C) There is at least one character between the "@" and the period

public boolean isValid(String email) {

    int loc = email.indexOf("@");
    
    // If there is no "@" character, then return false.
    if (loc == -1) {
        return false;

    // Now get the location of the first period after the "@"
    int periodLoc = email.indexOf(".", loc);
   
    // If there is not a period and it is not more that one character away.
    if (periodLoc - loc <= 1) {
        return false;

    return true;
}



Edit: Also, you can look into regex, which makes it easy to match Strings to a set of constraints. Look into the tutorial here:
http://java.sun.com/...ssential/regex/

Also, here is a snippet on Emails in regex:
http://www.dreaminco...snippet3391.htm

This post has been edited by Dogstopper: 09 March 2010 - 08:00 PM

Was This Post Helpful? 0
  • +
  • -

#15 zim1985  Icon User is offline

  • Grand Inquisitor
  • member icon

Reputation: 74
  • View blog
  • Posts: 568
  • Joined: 19-February 10

Re: Email Validation

Posted 09 March 2010 - 08:15 PM

View PostDogstopper, on 09 March 2010 - 05:58 PM, said:

zim, an InputMismatchException is NOT going to determine whether the address is valid...

I was writing to validate that the email address what a String...crud. My bad. I'm having an off day... :/
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2