I guess what you need is to validate the email, for that java has its established method, which is quite useful (I sense it because I try to do it that way too and the laughs)
The method in question is isValidEmailAddress
and it is already responsible for cleaning the String that you have to enter as a parameter, if it does not exist you have to capture the exception
public static boolean isValidEmailAddress(String email) {
boolean result = true;
try
{
InternetAddress emailAddr = new InternetAddress(email);
emailAddr.validate();
}
catch (AddressException ex)
{
result = false;
}
return result;
}
And if you do not want to use the methods provided by java. You can use a standardized pattern, which I add below
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Main {
public static void main(String[] args) {
// Patrón para validar el email
Pattern pattern = Pattern
.compile("^[_A-Za-z0-9-\+]+(\.[_A-Za-z0-9-]+)*@"
+ "[A-Za-z0-9-]+(\.[A-Za-z0-9]+)*(\.[A-Za-z]{2,})$");
// El email a validar
String email = "[email protected]";
Matcher mather = pattern.matcher(email);
if (mather.find() == true) {
System.out.println("El email ingresado es válido.");
} else {
System.out.println("El email ingresado es inválido.");
}
}