gpt4 book ai didi

java - 如何通过组合两个不同的变量来创建变量名

转载 作者:行者123 更新时间:2023-11-30 05:36:11 25 4
gpt4 key购买 nike

我从 CSV 文件中获取值,然后将其存储在字符串中,现在这些字符串是我使用 Selenium 中的 sendKeys 传递的数据。现在的问题是我想将这些 String 的变量与 for 循环计数结合起来,那么我该怎么做呢?

String csvFile = "/home/Miscellaneous/demo.csv";
CSVReader csv_reader = new CSVReader(new FileReader(csvFile));
List<String[]> strings = csv_reader.readAll();
for(int dime=2 ;dime<strings.size() ;dime++)
{
//Get data from csv file
String[] csvCell = strings.get(dime);
String no_of_new_rows_temp = csvCell[2];
Integer no_of_new_rows = Integer.valueOf(no_of_new_rows_temp);
String dim_label_1 = csvCell[3];

for (int no_of_new_rows_fill = 0; no_of_new_rows_fill <= no_of_new_rows; no_of_new_rows_fill++)
{
//Add Dimentions
driver.findElement(By.name("dimension[label]["+no_of_new_rows_fill+"]")).sendKeys("dim_label_" + no_of_new_rows_fill);
}
}

上面的代码确实返回了我的期望,但它是一个字符串,而我需要它作为一个变量。

我的预期输出应该是这样的:sendKeys 中的“dim_label_”和“no_of_new_rows_fill”应该创建

dim_label_1

通过它我可以将dim_label_1的CSV文件数据发送到字段。

最佳答案

在java中创建动态变量是不可能的。但是你可以利用这里的java集合来解决这个问题

 String csvFile = "/home/Miscellaneous/demo.csv";
CSVReader csv_reader = new CSVReader(new FileReader(csvFile));
List<String[]> strings = csv_reader.readAll();
Map<String,String> map = new HashMap()<String,String>;
for(int dime=2 ;dime<strings.size() ;dime++)
{
//Get data from csv file
String[] csvCell = strings.get(dime);
String no_of_new_rows_temp = csvCell[2];
Integer no_of_new_rows = Integer.valueOf(no_of_new_rows_temp);
//String dim_label_1 = csvCell[3];
map.put("dim_label"+dime,csvCell[3]);
for (int no_of_new_rows_fill = 0; no_of_new_rows_fill <= no_of_new_rows; no_of_new_rows_fill++)
{
//Add Dimentions
//driver.findElement(By.name("dimension[label]["+no_of_new_rows_fill+"]")).sendKeys("dim_label_" + no_of_new_rows_fill);
driver.findElement(By.name("dimension[label]["+no_of_new_rows_fill+"]")).sendKeys(map.get("dim_label_" + no_of_new_rows_fill));
}
}

关于java - 如何通过组合两个不同的变量来创建变量名,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56558779/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com