我正在尝试读取 Excel 单元格值,然后尝试将 Excel 单元格值与 txt 文件中存在的值进行匹配。
问题是,当执行以下代码时,它仅将 Excel 单元格中的 1675683811 值与 DATA.txt 中的 1675683811 值匹配,然后对于其他三个值不匹配。如何将所有四个值与 DATA.txt 文件匹配。
在一个特定的 Excel 单元格中,值如下所示:
String meid = "1675683811,2002199221,3893245956,9184020971";
txt 文件中的值如下所示:
"1675683811","590483002",
"2002199221","876015525",
"3893245956","502139683",
"9184020971","1029595777",
以下是我的代码:
static int i = "";
File objFile = new File("C:\\DATA.txt");
BufferedReader br = new BufferedReader(new FileReader(objFile));
String st;
String[] memid =meid.split(",");
int matchValue = 0;
for(i = 0;i<memid.length;i++){
matchValue = 0;
System.out.println("Memberid is :"+memid[i]);
while ((st = br.readLine()) != null){
if (st.toString().contains(memid[i].toString())){
matchValue++;
} else {
}
}
if (matchValue != 0) {
objReport.setValidationMessageInReport("PASS" , memid[i] + " text Value is Matched");
} else {
// continue;
objReport.setValidationMessageInReport("FAIL" , memid[i] + " text Value is not Matched");
}
}
最佳答案
您尝试在 for
循环内读取文件。一旦文件被读取/头到达文件末尾,BufferedReader 始终返回 null。即,在 for
(i>=1
) 循环的第一次迭代之后,while 循环始终返回 null
。我会在 for
循环之外读取该文件,将其存储在一个数组中,然后检查您的 Excel 行元素是否包含
该字符串。
像这样的事情:
int i = 0;
File objFile = new File("C:\\DATA.txt");
BufferedReader br = new BufferedReader(new FileReader(objFile));
String st;
String strMemId = "1675683811,2002199221,3893245956,9184020972";
String[] memid =strMemId.split(",");
int matchValue = 0;
String fromFile = "";
while ((st = br.readLine()) != null)
{
fromFile += st;
}
for(i = 0;i<memid.length;i++)
{
matchValue = 0;
if (fromFile.contains(memid[i].toString())){
matchValue++;
}
System.out.println("Memberid is :"+memid[i]);
if (matchValue != 0) {
objReport.setValidationMessageInReport("PASS" , memid[i] + " text Value is Matched");
} else {
// continue;
objReport.setValidationMessageInReport("FAIL" , memid[i] + " text Value is not Matched");
}
}
br.close();
希望这有帮助:)
关于java - 迭代excel值并与txt文件值匹配,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51828243/