Skip to main content

I have a dilemma I am trying to find out the best way to compare a string variable called (code) to an array of strings. if it equal it i want it to break the for loop. Which would should I select. I think the 2nd one will work but the 1st one seems like it would and its simpler. Any advice would be appreciated.

String[] badcodes = {"8QQ", "8BQ", "8JQ"}; if (code.equals(badcodes)) { break; } String[] badcodess = {"8QQ", "8BQ", "8JQ"}; for (String s : badcodess) {   if (s.equals(code)) { break; // break out of for loop } } 

--------------based on answer ----------------------

 String[] badcodes = {"8QQ", "8BQ", "8JQ"};  boolean check = Arrays.asList(badcodess).contains(code);  if (check = true) { // do something  } else { // do something  } 

I have a dilemma I am trying to find out the best way to compare a string variable called (code) to an array of strings. if it equal it i want it to break the for loop. Which would should I select. I think the 2nd one will work but the 1st one seems like it would and its simpler. Any advice would be appreciated.

String[] badcodes = {"8QQ", "8BQ", "8JQ"}; if (code.equals(badcodes)) { break; } String[] badcodess = {"8QQ", "8BQ", "8JQ"}; for (String s : badcodess) {   if (s.equals(code)) { break; // break out of for loop } } 

--------------based on answer ----------------------

 String[] badcodes = {"8QQ", "8BQ", "8JQ"};  boolean check = Arrays.asList(badcodess).contains(code);  if (check = true){ // do something  }else{ do something  } 

I have a dilemma I am trying to find out the best way to compare a string variable called (code) to an array of strings. if it equal it i want it to break the for loop. Which would should I select. I think the 2nd one will work but the 1st one seems like it would and its simpler. Any advice would be appreciated.

String[] badcodes = {"8QQ", "8BQ", "8JQ"}; if (code.equals(badcodes)) { break; } String[] badcodess = {"8QQ", "8BQ", "8JQ"}; for (String s : badcodess) { if (s.equals(code)) { break; // break out of for loop } } 

--------------based on answer ----------------------

String[] badcodes = {"8QQ", "8BQ", "8JQ"}; boolean check = Arrays.asList(badcodess).contains(code); if (check = true) { // do something } else { // do something } 
Post Undeleted by Youcef LAIDANI, Eran, davidxxx
Post Deleted by Sotirios Delimanolis, Nkosi, Mike M.
Post Undeleted by Eran, Youcef LAIDANI, davidxxx
Post Deleted by Sotirios Delimanolis, Machavity, techraf
deleted 2117 characters in body
Source Link
Jonathan
  • 385
  • 2
  • 9
  • 29

running into an additional problem The code is not finding the string that equals 8QQ which is a match here is my full code.

 String selector = "div#companyIdBarCompListGrid_rows_scrollpane table tbody tr[id*=companyIdBarCompListGrid_] td span div a"; int links = driver.findElements(By.cssSelector(selector)).size(); System.out.println("Number of links: " + links); // begin inner for-loop for (int i = 0; i < links; i++) { PP_OBJ_CycleData.ScrollToTop(driver);// scroll up List<WebElement> CCTable = driver.findElements(By.cssSelector(selector)); WebElement code = CCTable.get(i); System.out.println("\n"+code.getText().substring(0, 3).trim()+"\n"); //code.click(); //----------------------checking for bad code ----------------------------------------- String[] badcodes = {"8QQ", "8BQ", "8JQ"}; boolean check = Arrays.asList(badcodes).contains(code); System.out.println(check); //check == true will work as well if(check){ System.out.println(check+"Bad Code found breaking loop"); break; }else{ //checking to make sure element is clickable PP_OBJ_CycleData.isClickable(code, driver); System.out.println("Clickable?"+ code.isEnabled()); code.click(); } 

I think the problem is this part here:

List<WebElement> CCTable = driver.findElements(By.cssSelector(selector)); WebElement code = CCTable.get(i); System.out.println("\n"+code.getText().substring(0, 3).trim()+"\n"); //code.click(); 

I am cutting off everything but the first 3 characters with sub string but that is during the print statement. however the string is actually longer or has more characters than what is in the array for 8QQ. Could this be the problem if so is there a way to get around it?


running into an additional problem The code is not finding the string that equals 8QQ which is a match here is my full code.

 String selector = "div#companyIdBarCompListGrid_rows_scrollpane table tbody tr[id*=companyIdBarCompListGrid_] td span div a"; int links = driver.findElements(By.cssSelector(selector)).size(); System.out.println("Number of links: " + links); // begin inner for-loop for (int i = 0; i < links; i++) { PP_OBJ_CycleData.ScrollToTop(driver);// scroll up List<WebElement> CCTable = driver.findElements(By.cssSelector(selector)); WebElement code = CCTable.get(i); System.out.println("\n"+code.getText().substring(0, 3).trim()+"\n"); //code.click(); //----------------------checking for bad code ----------------------------------------- String[] badcodes = {"8QQ", "8BQ", "8JQ"}; boolean check = Arrays.asList(badcodes).contains(code); System.out.println(check); //check == true will work as well if(check){ System.out.println(check+"Bad Code found breaking loop"); break; }else{ //checking to make sure element is clickable PP_OBJ_CycleData.isClickable(code, driver); System.out.println("Clickable?"+ code.isEnabled()); code.click(); } 

I think the problem is this part here:

List<WebElement> CCTable = driver.findElements(By.cssSelector(selector)); WebElement code = CCTable.get(i); System.out.println("\n"+code.getText().substring(0, 3).trim()+"\n"); //code.click(); 

I am cutting off everything but the first 3 characters with sub string but that is during the print statement. however the string is actually longer or has more characters than what is in the array for 8QQ. Could this be the problem if so is there a way to get around it?

added 2115 characters in body
Source Link
Jonathan
  • 385
  • 2
  • 9
  • 29

running into an additional problem The code is not finding the string that equals 8QQ which is a match here is my full code.

 String selector = "div#companyIdBarCompListGrid_rows_scrollpane table tbody tr[id*=companyIdBarCompListGrid_] td span div a"; int links = driver.findElements(By.cssSelector(selector)).size(); System.out.println("Number of links: " + links); // begin inner for-loop for (int i = 0; i < links; i++) { PP_OBJ_CycleData.ScrollToTop(driver);// scroll up List<WebElement> CCTable = driver.findElements(By.cssSelector(selector)); WebElement code = CCTable.get(i); System.out.println("\n"+code.getText().substring(0, 3).trim()+"\n"); //code.click(); //----------------------checking for bad code ----------------------------------------- String[] badcodes = {"8QQ", "8BQ", "8JQ"}; boolean check = Arrays.asList(badcodes).contains(code); System.out.println(check); //check == true will work as well if(check){ System.out.println(check+"Bad Code found breaking loop"); break; }else{ //checking to make sure element is clickable PP_OBJ_CycleData.isClickable(code, driver); System.out.println("Clickable?"+ code.isEnabled()); code.click(); } 

I think the problem is this part here:

List<WebElement> CCTable = driver.findElements(By.cssSelector(selector)); WebElement code = CCTable.get(i); System.out.println("\n"+code.getText().substring(0, 3).trim()+"\n"); //code.click(); 

I am cutting off everything but the first 3 characters with sub string but that is during the print statement. however the string is actually longer or has more characters than what is in the array for 8QQ. Could this be the problem if so is there a way to get around it?


running into an additional problem The code is not finding the string that equals 8QQ which is a match here is my full code.

 String selector = "div#companyIdBarCompListGrid_rows_scrollpane table tbody tr[id*=companyIdBarCompListGrid_] td span div a"; int links = driver.findElements(By.cssSelector(selector)).size(); System.out.println("Number of links: " + links); // begin inner for-loop for (int i = 0; i < links; i++) { PP_OBJ_CycleData.ScrollToTop(driver);// scroll up List<WebElement> CCTable = driver.findElements(By.cssSelector(selector)); WebElement code = CCTable.get(i); System.out.println("\n"+code.getText().substring(0, 3).trim()+"\n"); //code.click(); //----------------------checking for bad code ----------------------------------------- String[] badcodes = {"8QQ", "8BQ", "8JQ"}; boolean check = Arrays.asList(badcodes).contains(code); System.out.println(check); //check == true will work as well if(check){ System.out.println(check+"Bad Code found breaking loop"); break; }else{ //checking to make sure element is clickable PP_OBJ_CycleData.isClickable(code, driver); System.out.println("Clickable?"+ code.isEnabled()); code.click(); } 

I think the problem is this part here:

List<WebElement> CCTable = driver.findElements(By.cssSelector(selector)); WebElement code = CCTable.get(i); System.out.println("\n"+code.getText().substring(0, 3).trim()+"\n"); //code.click(); 

I am cutting off everything but the first 3 characters with sub string but that is during the print statement. however the string is actually longer or has more characters than what is in the array for 8QQ. Could this be the problem if so is there a way to get around it?

added 244 characters in body
Source Link
Jonathan
  • 385
  • 2
  • 9
  • 29
Loading
Post Closed as "Duplicate" by Sotirios Delimanolis java
deleted 125 characters in body
Source Link
Youcef LAIDANI
  • 60.3k
  • 21
  • 111
  • 178
Loading
Source Link
Jonathan
  • 385
  • 2
  • 9
  • 29
Loading