2 回答
TA贡献1995条经验 获得超2个赞
double count = 0, countBuffer = 0, countLine = 0;
String lineNumber = "";
String filePath = "D:\\PDFTOEXCEL\\Extractionfrompdf.txt";
BufferedReader br;
String inputSearch = "Facture";
String line = "";
List<String> searchedWords = new ArrayList<>();
try {
br = new BufferedReader(new FileReader(filePath));
try {
while ((line = br.readLine()) != null) {
countLine++;
//System.out.println(line);
String[] words = line.split(" ");
for (String word : words) {
if (word.equals(inputSearch)) {
count++;
countBuffer++;
if(!searchedWords.contains(word)){
searchedWords.add(word);
}
}
}
if (countBuffer > 0) {
countBuffer = 0;
lineNumber += countLine + ",";
}
}
br.close();
} catch (IOException e) {
e.printStackTrace();
}
} catch (FileNotFoundException e) {
e.printStackTrace();
}
System.out.println("Words that you have searched and found:");
for(String word : searchedWords){
System.out.println(word);
}
您将拥有一个searchedWords跟踪搜索单词的数组列表,您会注意到 if 语句不允许在数组列表中重复,因此如果您想允许重复,只需删除if(!searchedWords.contains(word))并写入searchedWords.add(word);.
TA贡献1900条经验 获得超5个赞
您可以使用其他声明定义一个 String ArrayList:
ArrayList<String> matches = new ArrayList<String>();
然后,当您找到匹配的单词时,使用以下命令将其添加到 ArrayList:
matches.add(word);
编辑:
double count = 0, countBuffer = 0, countLine = 0;
String lineNumber = "";
String filePath = "D:\\PDFTOEXCEL\\Extractionfrompdf.txt";
BufferedReader br;
String inputSearch = "Facture";
String line = "";
ArrayList<String> matches = new ArrayList<String>();
try {
br = new BufferedReader(new FileReader(filePath));
try {
while ((line = br.readLine()) != null) {
countLine++;
//System.out.println(line);
String[] words = line.split(" ");
for (String word : words) {
if (word.equals(inputSearch)) {
count++;
countBuffer++;
matches.add(word);
}
}
if (countBuffer > 0) {
countBuffer = 0;
lineNumber += countLine + ",";
}
}
br.close();
} catch (IOException e) {
e.printStackTrace();
}
} catch (FileNotFoundException e) {
e.printStackTrace();
}
添加回答
举报