2

I have the following code.

for (String str5 : verticesposition2) {
    if(!str5.contains(("Vertex")||("Name")||("Transmittance")) {
        System.out.println(str5); 
    }                           
}

As you can see above if the string does NOT contain Vertex, Name or Transmittance I want it to print out. However Im getting a compilation error saying that the || operator is undefined for the argument types. I'm relatively new to programming so Im not sure what this means could someone kindly point in the right direction on how to fix my code?

1

3 Answers 3

17

Java doesn't have a syntax like that, but you can put the "or" in a regex:

if (!str5.matches(".*(Vertex|Name|Transmittance).*")) {

Note that java's matches() (unlike many other languages) must match the whole string to return true, hence the .* at each end of the regex.

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

Comments

6

The || operator works on individual boolean terms, not to provide a bunch of different arguments.

if((!str5.contains("Vertex")||!str5.contains("Name")||!str5.contains("Transmittance")){

2 Comments

its logically wrong. it should be: if((!str5.contains("Vertex")&&!str5.contains("Name")&&!str5.contains("Transmittance")) because the OP want to know if the string contain not one of the words.
@WhoAmI His statement is true if the str5 not contains "Vertext" for example, but it should only be true if str5 not contains any of the three words-
1

try to use this code

 public static void main(String[] args) {
        List<String> verticesposition2 = new ArrayList<String>();
        verticesposition2.add("safdsfVertex");
        verticesposition2.add("safdsfNamesfsd");
        verticesposition2.add("notCONTAINS");

        for (String str5 : verticesposition2){
            if(!(str5.contains("Vertex")||str5.contains("Name")||str5.contains("Transmittance"))){
                System.out.println(str5);
            }
        }
    }

Output:

notCONTAINS

if speed isn't critical -- use those with regexp.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.