简体   繁体   中英

Comparing two set of strings

Here is my problem. I'm trying to compare two different string by using && and also .equals but seems like it cannot give me the result it should be.

Here is my code (starting from where I think is the problem is) :

    for(int i = 0; i < datalist.size(); i+=3)   {       

        String temp1 = datalist.get(i);
        String temp2 = datalist.get(i+1);

        System.out.println(temp1);
        System.out.println(temp2);

        if (temp1.equals(dataquery1)) {

            System.out.println("TRUE");

            if (temp2.equals(dataquery2))   {

            System.out.println("TRUE");
            array2.add((datalist.get(i)));
            array2.add((datalist.get(i+1)));
            array2.add((datalist.get(i+2)));

            }   
        }

    }

    System.out.println("\n\nArray2 size : " + array2.size());
    for (int j = 0; j < array2.size(); j++) {

        System.out.println("Array2 : " + array2.get(j));

    }

This is the array :

[0]  Lipase B
[1]  X-33
[2]  pPICZ?A
[3]  Candida antarctica lipase B (CALB)
[4]  SMD1168H
[5]  pGAP?B
[6]  Lip 2
[7]  X-33
[8]  pPICZ?A

And the result is :

Lipase B
X-33
Candida antarctica lipase B (CALB)
SMD1168H
Lip 2
X-33


Array2 size : 0

The result it should be is :

TRUE
TRUE

Array2 size : 3
Array2 : Lipase B
Array2 : X-33
Array2 : pPICZ?A

I tried using if (temp1.equals(dataquery1) && temp2.equals(dataquery2)) but it doesn't work. However if I change the dataquery1 and dataquery2 with its value Lipase B and X-33 respectively, the code works fine.

Can anyone help?

By the looks of things, you're setting dataquery1 to "Lipase" when you should be setting it to "Lipase B". If you fix that, I get the correct output:

Lipase B
X-33
TRUE
TRUE
Candida antarctica lipase B (CALB)
SMD1168H
Lip 2
X-33


Array2 size : 3
Array2 : Lipase B
Array2 : X-33
Array2 : pPICZ?A

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM