You are viewing our Forum Archives. To view or take place in current topics click here.
Email Validity Checker Program Help
Posted:

Email Validity Checker Program HelpPosted:

DrSmitty
  • Challenger
Status: Offline
Joined: Jun 01, 201311Year Member
Posts: 173
Reputation Power: 6
Status: Offline
Joined: Jun 01, 201311Year Member
Posts: 173
Reputation Power: 6
TTG, need some help to finish and properly setup the rest of my code for this email checker program. It was given as a quick assignment to work on setting up my code efficiently into methods and to effectively put together these segments of code as a whole program in separate method checks.

The doc file, explaining instructions and the gist of this program:
Email Validity Checker Program

This program should decide whether a given email address is valid or not based on the following 5 rules.

1) Email addresses may not have spaces or symbols, except for underscores, dashes, dots and the @ symbol.
2) They must have 1 and only 1 @ symbol.
3) They must contain at least one dot AFTER the @ symbol. If there is no @ symbol, you can automatically fail this rule. If there is more than one @ symbol, then verify that there is at least one dot after the first one.
4) They may not begin with a number or any symbol
5) They must end with one of the following tags: com, org, net, gov, biz (yes I know there are plenty more, but we are only going to check for these 5).

The program only has to evaluate one email address:

Input/Output:

Enter email address: tom currier3@[email protected]
Rule 1: fail
Rule 2: fail
Rule 3: pass
Rule 4: pass
Rule 5: fail
This is not a valid email address

Examples:

Enter email address: tom.crab @ yahoo.com
This is a valid email address.

Enter email address: tom_crab-is-a-pig @ server.pig.gov
This is a valid email address.

Enter email address: tom.crab.is.a.pig @ x.org
This is a valid email address.

Enter email address: tom.crab.is.a.pig @ x.x
This is not a valid email address.

Enter email address: tom crab @ yahoo.com
This is not a valid email address.

Enter email address: 2crabs @ yahoo.com
This is not a valid email address.

Enter email address: tom@yahoo
This is not a valid email address.

Enter email address: tom.crab@yahoo@com
This is not a valid email address.

Enter email address: tom.crab.yahoo.com
This is not a valid email address.


Here is my code, what I have so far(i have to organize my code accordingly with methods to split up different tasks):
import java.util.Scanner;
public class emailCheck {

   /**
    * @param args
    */
   public static void main(String[] args) {

      Scanner kb = new Scanner(System.in);
      System.out.print("Enter your email adress: ");
      String email = kb.nextLine();
      
       //boolean rules [] = {true, true, true, true, true};
             
      for (int i = 0; i < email.length(); i++)
         ok(email.charAt(i));
         
      
      

   }
   
   public static boolean ok(char c)
    {
      
      if (c >= 'A' && c <= 'Z')
          return true;
       
       else if (c >= 'a' && c <= 'z')
           return true;
       
       else if (c >= '0' && c <= '9')
           return true;
       
       else if (c == '.' || c == '-' || c == '_')
           return true;
       
       return false;
    }

}



Any and all help is greatly appreciated. I will give rep to all who help out and give meaningful input!

Thanks,
DrSmitty
#2. Posted:
Fold
  • Moderator
Status: Offline
Joined: Oct 01, 201112Year Member
Posts: 2,842
Reputation Power: 19685
Motto: Brandon has stopped paying for his motto advertising space. This motto is now vacant.
Motto: Brandon has stopped paying for his motto advertising space. This motto is now vacant.
Status: Offline
Joined: Oct 01, 201112Year Member
Posts: 2,842
Reputation Power: 19685
Motto: Brandon has stopped paying for his motto advertising space. This motto is now vacant.
Made a long winded approach in .NET, hopefully the logic will help you out:

 Dim characters As String = "[email protected]_"
        Dim FirstChar As String = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
        Dim val As String = InputBox("Enter Email").ToUpper
        Dim AtCount As Integer = 0
        Dim DotCount As Integer = 0

        If IsNumeric(val(0)) Or Not FirstChar.Contains(val(0)) Then
            MsgBox("You are breaking Rule 4!")
            Exit Sub
        End If
        'Rule 4 is passed here
        For i As Integer = 0 To val.Length - 1
            If Not characters.Contains(val(i)) Then
                MsgBox("You are breaking Rule 1!")
                Exit Sub
            End If
            'Rule 1 is passed here
            If val(i) = "@" Then
                AtCount += 1
                If AtCount > 1 Then
                    MsgBox("You are breaking Rule 2!")
                    Exit Sub
                End If
            End If
            'Rule 2 is passed here
            If val(i) = "." Then

                If AtCount > 0 And AtCount < 2 Then
                    DotCount += 1
                    'Rule 3 is passed here
                    Dim extensions As String() = {"COM", "ORG", "NET", "GOV", "BIZ"}
                    Dim domain As String() = val.Split(".")

                    If Not domain(domain.Length - 1) = extensions(0) Or domain(1) = extensions(1) Or domain(1) = extensions(2) Or domain(1) = extensions(3) Or domain(1) = extensions(4) Then
                        MsgBox("You are breaking Rule 5!")
                        Exit Sub
                    End If
                    'Rule 5 is passed here
                End If
            End If

        Next
        If DotCount = 1 Then
            MsgBox("Email is valid!")
        Else

            MsgBox("You are breaking Rule 3!")
        End If


So what I basically did was I asked for user input (val) and then the first thing I did was I checked to see if the first character was a number (isNumeric) or wasn't in the alphabet. If it passed that test, I moved on into a long for loop that checked the rest. I checked every single character in the email to make sure it was either a letter, number, or an allowed symbol, if at any point it wasn't, I told the user they broke Rule 1. If the character passed Rule 1, I checked specifically to see if it was a "@" symbol. If it was, I added 1 to a integer I had called "AtCount". If at any point AtCount was more than 1 (signifying that there was more than 1 "@" symbol), I told the user that they were breaking rule 2. The next thing I did was checked if the character was a "." symbol. If it was, and if we had already recognized that there was an "@" symbol already in the string, then I split the original input by this "." symbol and added 1 to the integer "DotCount". If the string that was AFTER the "." was either com, biz, org, net, or gov, then the user passed Rule 5 AND Rule 3 since the remaining characters in the string are com, biz, etc. If the string didn't equal any of the domains, then the user was told they broke Rule 5. Near the end, before the program told the user the email was valid, it checked to make sure that "DotCount" was equal to one, to make sure there was indeed a period after the "@" symbol. If there wasn't, the user was told they broke Rule 3.

I know it isn't the language you are working in, but hopefully some of it helped. I'll be up for a bit longer so if you need anything just post or pm me .
Jump to:
You are viewing our Forum Archives. To view or take place in current topics click here.