r/javahelp • • Oct 08 '20

Email checker

So i have a long comprehensive lab and i need to check if a email has .edu inside of it ive been trying mainly for() but thats all i dont really get how to check if some one can lead me in the right direction i would appreciate it.

22 Upvotes

14 comments sorted by

7

u/kAlvaro Oct 08 '20 edited Oct 08 '20

For what it's worth, you may also want to validate the email format altogether:

import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;
public static boolean isValidAddress(String email) {
    try {
        InternetAddress internetAddress = new InternetAddress(email);
        internetAddress.validate();
        return true;
    } catch (AddressException e) {
        return false;
    }
}

1

u/SoIDKwhatmynameis Oct 09 '20

I appreciate the validation code but its not required but I now know more Thanks!

6

u/iamsadtbh Aspiring SWE Oct 08 '20

Assuming that the variable email is a string, take a look at the .contains method:

https://www.geeksforgeeks.org/java-string-contains-method-example/

1

u/SoIDKwhatmynameis Oct 08 '20

Thank you that was my solution

2

u/blackkkmamba Oct 08 '20

Careful that email.contains(".edu") might not be enough. For example "[email protected]".contains(".edu") is true, but the email is not valid.

2

u/SoIDKwhatmynameis Oct 09 '20

THANK YOU EVERYONE MUCH LOVE FOR THIS AMAZING COMMUNITY CANT WAIT TO HELP OTHERS LIKE YOU ARE NOW :)

18

u/tatanpoker09 Oct 08 '20

The most basic solution is using string.endsWith(“.edu”), however if this is for something more important you might wanna take a look at regex validation to check it is a valid email as well

1

u/[deleted] Oct 08 '20

I don’t really understand the assignment. are you taking in the e-mail as like a text document?

1

u/SoIDKwhatmynameis Oct 08 '20

I have a user input the Email and it the user inputted email doesnt have .edu its invalid

0

u/[deleted] Oct 08 '20 edited Oct 08 '20

you could probably just do an if statement with the .contains method like mentioned above. otherwise theres also .matches but that uses regex.

like if str.contains(“.edu”){

do stuff

}

if you can’t use .contains because you haven’t covered it in class yet or something a switch case could work.

4

u/AreTheseMyFeet Oct 08 '20

1

u/[deleted] Oct 08 '20

i forgot about symbols you might have to use .matches and exclude them with regular expressions

1

u/AreTheseMyFeet Oct 08 '20 edited Oct 08 '20

That's maybe a lot of work when you can just use the existing builtin String.endsWith(String).
However, using that you'd still only match against raw email addresses and not the wider acceptable formats such as Joe Bloggs <[email protected]> among other oddities allowed.

Personally I'd probably combine regex and String approaches.
Regex groups to pull out any email address portion from a String and then test that result with String.endsWith() though as seen in this thread, there's many valid/possible approaches to the original problem.

@see https://en.wikipedia.org/wiki/Email_address#Examples
@see https://stackoverflow.com/questions/162681/how-to-parse-formatted-email-address-into-display-name-and-email-address

1

u/Raph0007 Oct 08 '20

A lot of people pointed out how to simply do it with contains() or a regex, and those are all great ideas. However, you mentioned that you tried using a for loop. If you're interested in how to implement methods like contains() on your own (with a for loop), take a look at string search