-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

6
  • str != null && str.contains("@") Commented Jan 11, 2019 at 16:02
  • Actually it should be the other way round!!: str != null && str.contains("@") Commented Jan 11, 2019 at 16:04
  • @achAmháin if str is null, then this will throw NPE.. check for null first. Commented Jan 11, 2019 at 16:04
  • Yes - I realised that and have edited it cheers. Commented Jan 11, 2019 at 16:04
  • 1
    Why would you need java to do a front end validation? I think you are better off looking for a JavaScript answer. Commented Jan 11, 2019 at 16:16

5 Answers 5

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.

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

2 Comments

This answer is what I needed - how do I mark it as a verified answer?
Click the grey tick and it will become green.
0
String s = "[email protected]"; if(s!= null && !s.isEmpty() && s.contains("@") ) { System.out.println("ok"); } else System.out.println("ko"); } 

2 Comments

!s.isEmpty() is not needed. When s contains "@", it is never empty
yes, but he want to check if it's null, so I thought to check even if it was empty
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

Comments

0

Here is some really simple code to achieve this:

String ourString = "[email protected]"; 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

1 Comment

Oh wow, I did not know that was a thing. I'll have to get familiar with looking through that.
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 = "[email protected]"; 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 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.