1
public static void emailChecker() {
    Scanner input = new Scanner(System.in);
    String email = " ";
    char[] test;
    int counter = 0;

    System.out.println("Please enter your email: ");
    email = input.nextLine();

    test = email.toCharArray();

    for (int i = 0; i < email.length(); i++) {
        if (test[i] == 64 || test[i] == 46) {
            System.out.println("Email is valid");
        } else {
            System.out.println("Email is not valid");
        }
    }

}

I figured out that on line 10 the output will say email is valid if the string contains either a "." or a "@". But I want my code to only say that the string is valid when the "." comes after the "@". A sample of a valid email is: email@email.com.

Lance
  • 47
  • 3
  • Using regular expressions, you can make a much better validator: http://stackoverflow.com/questions/8204680/java-regex-email . See the answer in this. – J.N. May 09 '17 at 04:00
  • No...this isn't a very good way to do email validation. Instead, look into using regular expressions. There are _many_ other invalid inputs which your example doesn't bother to cover. – Tim Biegeleisen May 09 '17 at 04:01
  • I am only supposed to use arrays to check if it works. But I don't get the logic behind it. – Lance May 09 '17 at 04:07

3 Answers3

0

Try this, it will give you the output.

public static void emailChecker() {
        Scanner input = new Scanner(System.in);
        String email = " ";
        char[] test;
        int counter = 0;

        System.out.println("Please enter your email: ");
        email = input.nextLine();

        test = email.toCharArray();
        boolean valid = false;

        for (int i = 0; i < email.length(); i++) {
            if (test[i] == 64){
                for(int y=(i+1); y<email.length(); y++){
                    if(test[y] == 46){
                        valid = true;
                    }
                } 
            }
        }

        if(valid == true){
            System.out.println("Email is valid");
        }else{
            System.out.println("Email is not valid");
        }
}
AnjuT
  • 166
  • 1
  • 4
  • 17
0

RegEx are the simplest way to validate email id formats. If you want good working example, please refer

https://www.mkyong.com/regular-expressions/how-to-validate-email-address-with-regular-expression/

If you still want to go with char array comparison, here is a sample code using two additional int variable to have fine control over validations..

public static void emailChecker() {
    Scanner input = new Scanner(System.in);
    String email = " ";
    char[] test;
    System.out.println("Please enter your email: ");
    email = input.nextLine();
    test = email.toCharArray();

    int fountAtTheRateAt = -1;
    int fountDotAt = -1;

    for (int i = 0; i < email.length(); i++) {
        if (test[i] == 46) {
            fountDotAt = i;
        } else if (test[i] == 64) {
            fountAtTheRateAt = i;
        }
    }
    // at least 1 char in between @ and .
    if (fountDotAt != fountAtTheRateAt && (fountAtTheRateAt+ 1) < fountDotAt) {
        System.out.println("Email is valid");
    } else {
        System.out.println("Email is not valid");
    }
    input.close();
}
0

Here's one answer for your question using a loop.

But, as others have commented, this is not the way to validate email addresses.

boolean foundDot = false;
boolean foundAt = false;

for (char c: test) {
    if (!foundAt) {
        foundAt = (c == '@'); \\ the ( ) brackets are not required, but makes the code easier to read.
    } else {
        foundDot = (c == '.');
    }

    if (foundDot) {
        valid = true;
        break;
    }
}