'If text doesn't include one of these two (.com or .info), or if it doesnt include (@)

else if(!jTextField4.getText().endsWith(".com") || 
    !jTextField4.getText().endsWith(".info") || 
    !jTextField4.getText().contains("@"))

I tried using this code but it doesn't work. I need it for my college project using NetBeans. I am a beginner by the way.



Solution 1:[1]

You don't really have a question and you're not telling us the goal, so this is just a guess, but I suspect your logic is wrong. You probably want something more like

if (!(jTextField4.getText().endsWith(".com") || jTextField4.getText().endsWith(".info)) && !jtextField4.getText().contains("&"))

Solution 2:[2]

i'm not shure if the properties of String works directly on getText of a jField. And on the "else if" clausule you need to negate the individual value of each one, try this

// add this line before if-else 
String field4Str = jTextField4.getText();
//code...
else if ( 
    (! field4Str.endsWith(".com")) || 
    (! field4Str.endsWith(".info")) || 
    (! field4Str.contains("@"))
){
// code...
}

Solution 3:[3]

The pattern not case1 or not case2 or not... is wrong. As it is always true. (This was already commented: at least one case is false.) The || must be changed into &&s.

And introducing a variable for the repeated string helps reading, and might have helped.

Solution 4:[4]

else if((!jTextField4.getText().endsWith(".com") || 
!jTextField4.getText().endsWith(".info")) &&
!jTextField4.getText().contains("@"))

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Ryan
Solution 2 Marcio Rocha
Solution 3
Solution 4 Toomas Test