简体   繁体   中英

How to check condition in a for loop and print the statement once?

I have some nested code and I want to check it's condition and print the statement once. But I don't know how to reform the for loop or which is the ideal loop for this situstin to use.? I have written this and I am getting hello printed 4 times..I want it to be printed once.. Please help.

countDev = dev.findElements(
                By.xpath("html/body/form/div[2]/div[2]/ul/li")).size();
        countProd = prod.findElements(
                By.xpath("html/body/form/div[2]/div[2]/ul/li")).size();

        System.out.println(countDev);
        System.out.println(countProd);

        if (countDev == countProd) {

            for (int list = 1; list <= countDev; list++) {

                if (dev.findElement(
                        By.xpath("/html/body/form/div[2]/div[2]/ul/li[" + list
                                + "]/a/span"))
                        .getText()
                        .equals(prod.findElement(
                                By.xpath("/html/body/form/div[2]/div[2]/ul/li["
                                        + list + "]/a/span")).getText())) {


                }
                System.out.println("Hello!");

            }



        } else {

            System.out.println("Bye, Bye!");

        }

Not sure what you are trying to achieve, But just put a flag there and make it as true once print done.

boolean printed =false;
for (int list = 1; list <= countDev; list++) {
         if (your condtion here ) {
           if(!printed ){
             System.out.println("Hello!");
            printed = true;
             }

          }
     }

This loop will execute four times if countDev and countProd are equal to 4

for (int list = 1; list <= countDev; list++)

Also, your if statement is empty and your 'Hello' print statement is outside the if but inside the loop

Hlo. Buddy..I can't understood you clearly... try this code..

countDev = dev.findElements(
            By.xpath("html/body/form/div[2]/div[2]/ul/li")).size();
    countProd = prod.findElements(
            By.xpath("html/body/form/div[2]/div[2]/ul/li")).size();

    System.out.println(countDev);
    System.out.println(countProd);

    if (countDev == countProd) {

        for (int list = 1; list <= countDev; list++) {

            if (dev.findElement(
                    By.xpath("/html/body/form/div[2]/div[2]/ul/li[" + list
                            + "]/a/span"))
                    .getText()
                    .equals(prod.findElement(
                            By.xpath("/html/body/form/div[2]/div[2]/ul/li["
                                    + list + "]/a/span")).getText())) {
  System.out.println("Hello!");
break;


            }

        }



    } else {

        System.out.println("Bye, Bye!");

    }

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