-4

Basically, I have a form where people can enter stuff in, and there is one part where they input an email address. Sometimes, people just put in their name and don't put an actual email address. Sometimes they do not fill it out at all.

Is there any easy way to check to see if the string has an @ symbol in it and check to see if its Null?

Any help is appreciated

xearow
  • 3
  • 3

5 Answers5

1

Use an AND boolean operator.

str != null && str.contains("@")

We first check the string is not null, then check that it contains '@'. Note: The reason that the null check is first is so that we do not attempt to access the string if it is null.

James Conway
  • 339
  • 3
  • 20
0
String s = "email@email.it";
    if(s!= null && !s.isEmpty() && s.contains("@") ) {
        System.out.println("ok");
    }
    else System.out.println("ko");
}
Matteo Tomai
  • 174
  • 9
0

The String class contains a contains method to check the @ symbol, and you can simply check for null with a != null call:

public static void main(String[] args) {
    String a = "should be false";
    String b = "should be @true";
    String c = null;
    System.out.println(checkString(a));
    System.out.println(checkString(b));
    System.out.println(checkString(c));
}

static boolean checkString(String str) {
    return str != null && str.contains("@");
}

Output:

false

true

false

Community
  • 1
  • 1
achAmháin
  • 4,176
  • 4
  • 17
  • 40
0

Here is some really simple code to achieve this:

String ourString = "example@emailIsCool.com";
if(/* Check if our string is null: */ ourString != null &&
   /* Check if our string is empty: */ !ourString.isEmpty() &&
   /* Check if our string contains "@": */ ourString.contains("@")) {
    System.out.println("String Fits the Requirements");
} else {
    System.out.println("String Does Not Fit the Requirements");
}

For future reference, this is extremely broad for Stack Overflow, and you should try checking the javadoc before you make a post asking for the answer. For example, here is the javadoc for the String class: https://docs.oracle.com/javase/7/docs/api/java/lang/String.html

Javadocs provided by Oracle detail every method and attribute for all of the classes included in the standard java library. Here is a link to the javadoc homepage for Java 7. https://docs.oracle.com/javase/7/docs/api/overview-summary.html

Aidan Lovelace
  • 400
  • 2
  • 10
  • Oh wow, I did not know that was a thing. I'll have to get familiar with looking through that. – xearow Jan 11 '19 at 16:25
0

I think use Optional is the best way to do this.

Codes like this:

String nullEmail = null;
System.out.println(Optional.ofNullable(nullEmail).filter(s -> s.contains("@")).isPresent());

String rightEmail = "478309639@11.com";
System.out.println(Optional.ofNullable(rightEmail).filter(s -> s.contains("@")).isPresent());

String wrongEmail = "chentong";
System.out.println(Optional.ofNullable(wrongEmail).filter(s -> s.contains("@")).isPresent());

The output is:

false
true
false
TongChen
  • 1,414
  • 1
  • 11
  • 21