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

Time:02-03

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.

CodePudding user response:

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("&"))

CodePudding user response:

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...
}

CodePudding user response:

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.

CodePudding user response:

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