2

How do I use String.format() when the length of the String is unpredictable? I'm making a program that requires an email and the place where the "@" will vary depending on the length on what comes before it.

Edit: What I mean is, I need to check if the email format is valid. Example: [email protected] is a valid email, but doing johndoejohndoe,usa isn't valid. So I need to figure out if

  1. the format is valid
  2. finding out how to see if the format is valid with String.format() when the String length will vary depending on the email.
7
  • 6
    Please include some examples (emails that you would like to format) and the expected results? Commented Jun 28, 2015 at 7:25
  • A bit of a workaround would be to parse the string and manually look for the '@' delimiter. This can be done with a for-loop that traverses over each character. Commented Jun 28, 2015 at 7:39
  • String.format (..) does not use a @ token but uses special format specifiers such a %s for a string or %d for a number. See docs.oracle.com/javase/7/docs/api/java/util/… . It does not matter where these values appear in the String. They will be inserted in the place of the corresponding specifier. Commented Jun 28, 2015 at 10:06
  • @Gosu edited, read OP. Commented Jun 28, 2015 at 19:50
  • 1
    Ah! this is not String.format() String.format is for creating a formatted string not checking that the format is correct. I suggest that you work out what your rules are and manually check they are satisfied using other String methods like string.indexOf("@") and string.indexOf(".") Commented Jun 29, 2015 at 0:40

1 Answer 1

1

I am not entirely sure what you would deem as a valid email, but I did the following based on this assumption:

A valid email is a string that has at least 1 word character, followed by the '@' sign, followed by at least 1 alphabet, followed by the '.' character, and ending with at least 1 alphabet

Here's the code using regex:

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class QuickTester {

    private static String[] emails = {"[email protected]",
            "randomStringThatMakesNoSense",
            "abc@@@@@", "thisIsRegex@rubbish",
            "test123.com", "[email protected]",
            "@[email protected]"};

    public static void main(String[] args) {

        for(String email : emails) {
        System.out.printf("%s is %s.%n",
                email, 
                (isValidEmail(email) ? "Valid" : "Not Valid"));
        }   
    }

    // Assumes that domain name does not contain digits
    private static boolean isValidEmail (String emailStr) {

        // Looking for a string that has at least 1 word character,
        // followed by the '@' sign, followed by at least 1
        // alphabet, followed by the '.' character, and ending with
        // at least 1 alphabet
        String emailPattern = 
                "^\\w{1,}@[a-zA-Z]{1,}\\.[a-zA-Z]{1,}$";

        Matcher m = Pattern.compile(emailPattern).matcher(emailStr);
        return m.matches();
    }
}

Output:

[email protected] is Valid.
randomStringThatMakesNoSense is Not Valid.
abc@@@@@ is Not Valid.
thisIsRegex@rubbish is Not Valid.
test123.com is Not Valid.
[email protected] is Valid.
@[email protected] is Not Valid.

Based on your definition of a valid email, you could adjust the Pattern accordingly. I hope this helps!

Sign up to request clarification or add additional context in comments.

1 Comment

Yes, this is what I mean, thanks! Sorry, I can word things very complicatedly.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.