如何搜索分成两行的单词?
How to search for a word that is split in two lines?
我正在 java 中编写一个程序来搜索 .txt 文件中的单词列表(交易编号)。 .txt 文件可以有任意行数。
List<String> transactionList = new ArrayList<String>(
Arrays.asList("JQ7P00049", "TM7P04797", "RT6P70037");
FileReader fileReader = new FileReader(filePath);
BufferedReader bufferedReader = new BufferedReader(fileReader);
try {
String readLine = bufferedReader.readLine();
for (String transactionIndex : transactionList) {
if (readLine != null) {
if (readLine.contains(transactionIndex)) {
System.out.println(transactionIndex + ": true");
readLine = bufferedReader.readLine();
} else {
readLine = bufferedReader.readLine();
}
}
}
}
除非单词被分成两行,否则程序运行良好,例如:
-------- JQ7P0
0049------------
这显然是因为 bufferedReader 逐行读取并将搜索字符串与该行中存在的内容进行比较。
有什么办法可以处理这种情况吗?
正如 durron597 所提到的,您并没有遍历整个文件,但这里有一个解决方案,它假设文件至少有 2 行并且交易字符串不超过 2 行。
它将每一行与下一行连接起来,并在连接的行中搜索字符串。为了防止同一笔交易被打印两次,我添加了一张额外的支票。
List<String> transactionList = new ArrayList<String>( Arrays.asList("JQ7P00049", "TM7P04797", "RT6P70037") );
FileReader fileReader = new FileReader(filePath);
BufferedReader bufferedReader = new BufferedReader(fileReader);
try {
// Search the first line
String lastLine = bufferedReader.readLine();
for (String transactionIndex : transactionList) {
if (lastLine.contains(transactionIndex)) {
System.out.println(transactionIndex + ": true");
}
}
String currentLine = null;
// Search the remaining lines
while((currentLine=bufferedReader.readLine()) != null) {
String combined = lastLine + currentLine;
for (String transactionIndex : transactionList) {
if (currentLine.contains(transactionIndex) || (!lastLine.contains(transactionIndex) && combined.contains(transactionIndex))) {
System.out.println(transactionIndex + ": true");
}
}
lastLine = currentLine;
}
} catch ( Exception e ) {
System.out.println( e.getClass().getSimpleName() + ": " + e.getMessage() );
} finally {
bufferedReader.close();
}
这个程序还有第二个问题:您不会读取较长文件中的所有行,因为您没有循环遍历文件中的所有行。
也就是说,您可以通过一次读取两行并将它们合并在一起来完成此操作。
这是一个完整的程序:
private static final List<String> transactionList = new ArrayList<String>(Arrays.asList(
"JQ7P00049", "TM7P04797", "RT6P70037"));
public static void main(String[] args) throws Exception {
String filePath = "test.txt";
FileReader fileReader = new FileReader(filePath);
BufferedReader bufferedReader = new BufferedReader(fileReader);
try {
String firstLine = bufferedReader.readLine();
String secondLine = bufferedReader.readLine();
if (secondLine == null) {
checkLine(firstLine);
}
do {
String combinedLine = firstLine + secondLine;
checkLine(combinedLine);
firstLine = secondLine;
} while ((secondLine = bufferedReader.readLine()) != null);
} finally {
}
}
private static void checkLine(String combinedLine) {
for (Iterator<String> iterator = transactionList.iterator(); iterator.hasNext();) {
String transactionIndex = iterator.next();
if (combinedLine.contains(transactionIndex)) {
System.out.println(transactionIndex + ": true");
iterator.remove();
}
}
}
您的代码似乎没有正确读取文件,而是读取了与您要查找的交易编号一样多的行。假设这不是你想要的,我已经更正了。
此外,我假设一个交易号最多可以跨越两行。
List<String> transactionList = new ArrayList<String>(
Arrays.asList("JQ7P00049", "TM7P04797", "RT6P70037"));
FileReader fileReader = new FileReader(filePath);
BufferedReader bufferedReader = new BufferedReader(fileReader);
String[] lastLines = {"",""};
try {
String readLine;
while((readLine = bufferedReader.readLine()) != null) {
lastLines[0] = lastLines[1];
lastLines[1] = readLine;
String combinedLastLines;
combinedLastLines = lastLines[0] + lastLines[1];
for (String transactionIndex : transactionList) {
if (combinedLastLines.contains(transactionIndex) && !lastLines[0].contains(transactionIndex)) {
System.out.println(transactionIndex + ": true");
}
}
}
}
大体思路就是一直合并两行,看里面有没有交易号。让我们看一下代码:
String[] lastLines = {"",""};
这一行定义了一个数组,我们将用它来存储最近读取的两个行。
while((readLine = bufferedReader.readLine()) != null) {
此代码段读取的行数与您的文本文件中的行数相同。
lastLines[0] = lastLines[1];
lastLines[1] = readLine;
String combinedLastLines;
combinedLastLines = lastLines[0] + lastLines[1];
这段代码负责替换数组中最旧的行,并将当前读取的行压入数组。然后将最后两行合并为一个字符串!
if (combinedLastLines.contains(transactionIndex) && !lastLines[0].contains(transactionIndex)) {
我们在这里搜索交易编号的组合行。但是:当一个交易号不是跨行时,我们可能会不小心找到它两次。因此,第二次检查是为了确保我们之前没有找到交易。
希望这就是您要找的!
我正在 java 中编写一个程序来搜索 .txt 文件中的单词列表(交易编号)。 .txt 文件可以有任意行数。
List<String> transactionList = new ArrayList<String>(
Arrays.asList("JQ7P00049", "TM7P04797", "RT6P70037");
FileReader fileReader = new FileReader(filePath);
BufferedReader bufferedReader = new BufferedReader(fileReader);
try {
String readLine = bufferedReader.readLine();
for (String transactionIndex : transactionList) {
if (readLine != null) {
if (readLine.contains(transactionIndex)) {
System.out.println(transactionIndex + ": true");
readLine = bufferedReader.readLine();
} else {
readLine = bufferedReader.readLine();
}
}
}
}
除非单词被分成两行,否则程序运行良好,例如:
-------- JQ7P0
0049------------
这显然是因为 bufferedReader 逐行读取并将搜索字符串与该行中存在的内容进行比较。
有什么办法可以处理这种情况吗?
正如 durron597 所提到的,您并没有遍历整个文件,但这里有一个解决方案,它假设文件至少有 2 行并且交易字符串不超过 2 行。
它将每一行与下一行连接起来,并在连接的行中搜索字符串。为了防止同一笔交易被打印两次,我添加了一张额外的支票。
List<String> transactionList = new ArrayList<String>( Arrays.asList("JQ7P00049", "TM7P04797", "RT6P70037") );
FileReader fileReader = new FileReader(filePath);
BufferedReader bufferedReader = new BufferedReader(fileReader);
try {
// Search the first line
String lastLine = bufferedReader.readLine();
for (String transactionIndex : transactionList) {
if (lastLine.contains(transactionIndex)) {
System.out.println(transactionIndex + ": true");
}
}
String currentLine = null;
// Search the remaining lines
while((currentLine=bufferedReader.readLine()) != null) {
String combined = lastLine + currentLine;
for (String transactionIndex : transactionList) {
if (currentLine.contains(transactionIndex) || (!lastLine.contains(transactionIndex) && combined.contains(transactionIndex))) {
System.out.println(transactionIndex + ": true");
}
}
lastLine = currentLine;
}
} catch ( Exception e ) {
System.out.println( e.getClass().getSimpleName() + ": " + e.getMessage() );
} finally {
bufferedReader.close();
}
这个程序还有第二个问题:您不会读取较长文件中的所有行,因为您没有循环遍历文件中的所有行。
也就是说,您可以通过一次读取两行并将它们合并在一起来完成此操作。
这是一个完整的程序:
private static final List<String> transactionList = new ArrayList<String>(Arrays.asList(
"JQ7P00049", "TM7P04797", "RT6P70037"));
public static void main(String[] args) throws Exception {
String filePath = "test.txt";
FileReader fileReader = new FileReader(filePath);
BufferedReader bufferedReader = new BufferedReader(fileReader);
try {
String firstLine = bufferedReader.readLine();
String secondLine = bufferedReader.readLine();
if (secondLine == null) {
checkLine(firstLine);
}
do {
String combinedLine = firstLine + secondLine;
checkLine(combinedLine);
firstLine = secondLine;
} while ((secondLine = bufferedReader.readLine()) != null);
} finally {
}
}
private static void checkLine(String combinedLine) {
for (Iterator<String> iterator = transactionList.iterator(); iterator.hasNext();) {
String transactionIndex = iterator.next();
if (combinedLine.contains(transactionIndex)) {
System.out.println(transactionIndex + ": true");
iterator.remove();
}
}
}
您的代码似乎没有正确读取文件,而是读取了与您要查找的交易编号一样多的行。假设这不是你想要的,我已经更正了。
此外,我假设一个交易号最多可以跨越两行。
List<String> transactionList = new ArrayList<String>(
Arrays.asList("JQ7P00049", "TM7P04797", "RT6P70037"));
FileReader fileReader = new FileReader(filePath);
BufferedReader bufferedReader = new BufferedReader(fileReader);
String[] lastLines = {"",""};
try {
String readLine;
while((readLine = bufferedReader.readLine()) != null) {
lastLines[0] = lastLines[1];
lastLines[1] = readLine;
String combinedLastLines;
combinedLastLines = lastLines[0] + lastLines[1];
for (String transactionIndex : transactionList) {
if (combinedLastLines.contains(transactionIndex) && !lastLines[0].contains(transactionIndex)) {
System.out.println(transactionIndex + ": true");
}
}
}
}
大体思路就是一直合并两行,看里面有没有交易号。让我们看一下代码:
String[] lastLines = {"",""};
这一行定义了一个数组,我们将用它来存储最近读取的两个行。
while((readLine = bufferedReader.readLine()) != null) {
此代码段读取的行数与您的文本文件中的行数相同。
lastLines[0] = lastLines[1];
lastLines[1] = readLine;
String combinedLastLines;
combinedLastLines = lastLines[0] + lastLines[1];
这段代码负责替换数组中最旧的行,并将当前读取的行压入数组。然后将最后两行合并为一个字符串!
if (combinedLastLines.contains(transactionIndex) && !lastLines[0].contains(transactionIndex)) {
我们在这里搜索交易编号的组合行。但是:当一个交易号不是跨行时,我们可能会不小心找到它两次。因此,第二次检查是为了确保我们之前没有找到交易。
希望这就是您要找的!