Java 迭代excel值并与txt文件值匹配
我正在尝试读取excel单元格值,然后尝试将excel单元格值与txt文件中的值匹配 问题是,当执行以下代码时,它只匹配excel单元格中的1675683811值和DATA.txt中的1675683811值,而对于其他三个值,它不匹配。如何将所有四个值与DATA.txt文件匹配 在一个特定的excel单元格中,值如下所示:Java 迭代excel值并与txt文件值匹配,java,apache-poi,Java,Apache Poi,我正在尝试读取excel单元格值,然后尝试将excel单元格值与txt文件中的值匹配 问题是,当执行以下代码时,它只匹配excel单元格中的1675683811值和DATA.txt中的1675683811值,而对于其他三个值,它不匹配。如何将所有四个值与DATA.txt文件匹配 在一个特定的excel单元格中,值如下所示: String meid = "1675683811,2002199221,3893245956,9184020971"; "1675683811","590483002",
String meid = "1675683811,2002199221,3893245956,9184020971";
"1675683811","590483002",
"2002199221","876015525",
"3893245956","502139683",
"9184020971","1029595777",
在txt文件中,值如下所示:
String meid = "1675683811,2002199221,3893245956,9184020971";
"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");
}
}
static int i=“”;
File objFile=新文件(“C:\\DATA.txt”);
BufferedReader br=新的BufferedReader(新文件读取器(objFile));
字符串st;
字符串[]memid=meid.split(“,”);
int matchValue=0;
对于(i=0;i在内部while
循环中,您从文件中逐行读取,直到到达文件的末尾,一旦完成,当外部for
到达时,将不再读取任何内容,而循环第二次(以及第三次等等)时间br.readLine()
将始终返回null
我建议您更改循环中的位置,外部循环读取文件中的下一行,内部对excel数组进行匹配。您尝试在for
循环中读取文件。一旦读取文件/head到达文件末尾,BufferedReader
始终返回null。即,在第一个itera之后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();
inti=0;
File objFile=新文件(“C:\\DATA.txt”);
BufferedReader br=新的BufferedReader(新文件读取器(objFile));
字符串st;
字符串strMemId=“16756838112002199221389832459569184020972”;
字符串[]memid=strmeid.split(“,”);
int matchValue=0;
字符串fromFile=“”;
而((st=br.readLine())!=null)
{
fromFile+=st;
}
for(i=0;i当您在for循环的第一次迭代中使用while通过缓冲读取器读取文件时,您最终会消耗整个文件。它指向文件的结尾…因此在下一次迭代中它等于null,并且在while循环的第一次迭代中失败
while ((st = br.readLine()) != null){ // in the first iteration it finished reading the file
if (st.toString().contains(memid[i].toString())){
matchValue++;
} else {
}
这是你需要做的
static int i = "";
File objFile = new File("C:\\DATA.txt");
BufferedReader br = new BufferedReader(new FileReader(objFile));
String st;
String str="";
while ((st = br.readLine()) != null){
str+=st;
}
String[] memid =meid.split(",");
for(i = 0;i<memid.length;i++){
matchValue = 0;
System.out.println("Memberid is :"+memid[i]);
if (str.contains(memid[i].toString())) {
objReport.setValidationMessageInReport("PASS" , memid[i] + " text Value is Matched");
} else {
// continue;
objReport.setValidationMessageInReport("FAIL" , memid[i] + " text Value is not Matched");
}
}
static int i=“”;
File objFile=新文件(“C:\\DATA.txt”);
BufferedReader br=新的BufferedReader(新文件读取器(objFile));
字符串st;
字符串str=“”;
而((st=br.readLine())!=null){
str+=st;
}
字符串[]memid=meid.split(“,”);
对于(i=0;i