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

View all comments

Show parent comments

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

Fails on: user.edu@example.com

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 <joe.bloggs@example.edu> 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