1

Hey so I'm doing a project for school. We have to code a virtual atm machine. You would have to log in with your student mail.

My question is : How do I limit character length after a dot(.)?

public boolean validUsername(String username) {
        Boolean oneAT = false;
        for (int i=0; i < username.length(); i++) {
            if (username.contains("@") && username.contains(".") &&{
                oneAT = true;
            }
        }

        return oneAT;
    }

The function checks if the username typed, contains a @ and a .(dot). Is there a way to limit character length to three after the dot ? Otherwise the user can write johndoe@johndoemail.tugfksdoew

albert
  • 8,285
  • 3
  • 19
  • 32
gakkgreen
  • 15
  • 3
  • Well the user could also write `@johndoe.johndoe` which is not a valid address either. Catching the format of an email adress is a bit more complicated than that. – Alexis C. Nov 26 '14 at 20:28
  • 2
    This would be a great place to use a [regular expression](http://en.wikipedia.org/wiki/Regular_expression). – Obicere Nov 26 '14 at 20:28
  • please have a look a [regular expressions](https://docs.oracle.com/javase/tutorial/essential/regex/) and [quantifiers](https://docs.oracle.com/javase/tutorial/essential/regex/quant.html) – A4L Nov 26 '14 at 20:31
  • note that there are / will be top level domains with more than 3 characters. See http://en.wikipedia.org/wiki/Generic_top-level_domain#New_top-level_domains – geert3 Nov 26 '14 at 20:33
  • @Obicere the only problem is that the regular expression for true valid e-mail is very complicated. I'm pretty sure it's beyond the scope of a student. OP: do you have any specific instructions in your homework as to what would be considered valid e-mail? Valid emails include things like `john@somewhere.nz` and `a.b@john-doe.name` and `myname@mailserver.harvard-uni.edu`, so three letters won't do the trick. Never mind the more complicated rules of emails that allow pretty weird stuff to the left of the `@`. – RealSkeptic Nov 26 '14 at 20:35

3 Answers3

2

It's easier to validate the username with a regular expression

public boolean validUsername(String username) {
    Pattern pattern = Pattern.compile("^[_A-Za-z0-9-\\+]+(\\.[_A-Za-z0-9-]+)*@[A-Za-z0-9-]+(\\.[A-Za-z0-9]+)*(\\.[A-Za-z]{2,})$");
    Matcher matcher = pattern.matcher(username);
    return matcher.matches();
}

The expression validates if the username is a valid email address and returns true if so.

danielR
  • 297
  • 1
  • 3
0

To answer the specific question, you can limit the size by checking the size of the substring after the dot (assuming you only have on dot in the string):

afterDot = username.substring(username.indexOf("."))

But as stated by ZouZou, you should properly validate your email address (there's several other things to check for). Take a look at the example here: http://examples.javacodegeeks.com/core-java/util/regex/matcher/validate-email-address-with-java-regular-expression-example/

Sami Jaber
  • 51
  • 3
0

This should be helpful:

if (username.contains("@") && username.contains(".") && ((username.length() - username.lastIndexOf(".") <= 3)) {

Essentially it checks whether the difference of the String length and the last occurrence of dot is smaller or equal than 3. This handles emails like john.doe@yahoo.com as well.

Lajos Arpad
  • 64,414
  • 37
  • 100
  • 175