Home > Enterprise >  Is there a way to pass a string variable to a for loop in Java?
Is there a way to pass a string variable to a for loop in Java?

Time:05-17

My requirement is to delete data from a database, but the strings I need to pass to the DELETE query are dynamically generated, so I have to first collect the data and then use it in a for loop that would send the DELETE query multiple times.

My steps so far:

First, I filtered a list so as to collect a list of required elements into a variable. I got the expected output:

List<String> newFilter = allProductNames.stream().filter(field -> field.startsWith("sample", 1)).collect(Collectors.toList());
System.out.println(newFilter)

Output:

"sample1", "sample2", "sample3", "sample4"

Now, I want to use each of the elements to send a query to a Neo4j database, using the query - DROP DATABASE <database_name> wait; where <database_name> would be replaced by each of my collected elements. So the queries will look like this:

DROP DATABASE sample1 wait;
DROP DATABASE sample2 wait;
DROP DATABASE sample3 wait;
DROP DATABASE sample4 wait;
DROP DATABASE sample5 wait;

What comes straight to my mind is that a for loop can help me to send the query repeatedly by passing sample1 in the first query, sample2 in the second query...and so on, but my dilemma is that I am lost on how to achieve this.

I tried something like this, but it wont work:

List<String> newFilter = allProductNames.stream().filter(field -> field.startsWith("sample", 1)).collect(Collectors.toList());

    int filterSize = newFilter.size();

    int i = 0;
    String dropDatabase = "DROP DATABASE "   i   " wait;"; //i want to pass the element as a variable here

    for (; i < filterSize ; i  ) {

      basePage.queryInputField.sendKeys(dropDatabase);
    }

What I was trying to do in the loop is to pass make i = sample1 in the first run, i = sample2 in the second run and so on, but i is an int and I believe only an int can be passed into a condition as far as my little Java knowledge.

How can I pass the String sample1 to the loop in the first run to achieve this - DROP DATABASE sample1 wait; and pass the other elements in subsequent runs? I am open to any other approach than can help me achieve this.

CodePudding user response:

It does not work because you are not updating the dropDatabase String at any moment in the loop.

You have two options, use a for each or a for i updating the string:

for (int i = 0; i < filterSize; i  ) {
    basePage.queryInputField.sendKeys("DROP DATABASE "   newFilter.get(i)   " wait;");
}


for (String database : newFilter) {
    basePage.queryInputField.sendKeys("DROP DATABASE "   database   " wait;");
}

Or if you want the query string apart you could also use a format like this:

String format = "DROP DATABASE %s wait;";
for (String database : newFilter) {
    basePage.queryInputField.sendKeys(String.format(format, database));
}

CodePudding user response:

Try this:

for (String dbName : newFilter) {
    String dropDatabase = "DROP DATABASE "   dbName   " wait;";
    basePage.queryInputField.sendKeys(dropDatabase);
}
  •  Tags:  
  • java
  • Related