虽然循环似乎没有增加
While loop doesn't seem to be incrementing
我的代码基本上是这样的:
bufferedReader = new BufferedReader(new InputStreamReader(inputFile));
while ((currentLine = bufferedReader.readLine()) != null) {
Document document;
DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
document = dBuilder.parse(new InputSource(new StringReader(currentLine)));
document.getDocumentElement().normalize();
NodeList nList = document.getElementsByTagName("record");
int j = 0;
int k = 0;
while (j < lines) {
if (currentLine.contains(bList.get(k))) {
myList.add(currentLine);
bufferedReader.mark(0);
k++;
j++;
}
else if (myList.size() == bList.size()) {
break;
}
else {
j++;
}
}
我有一个 bufferedReader,它逐行浏览文件。我想要的是根据该行是否包含 bList 中存在的字符串,将单次出现的行添加到 myList。无论出于何种原因,当我调试时,尽管 k++ 递增,但 bList 不会移动到下一个索引。我也不确定在这里使用 bufferedReader.mark(0) 是否正确,我以前使用过 inputFile.getChannel().position(0);但我遇到了:
'Error: XML document structures must start and end within the same entity'
我在调试时注意到的另一个问题是,当 myList 和 bList 的大小相等时,while 循环不会中断。相反,myList 一直到 34 的大小,而 bList 的大小保持为 2。
EDIT: Added more lines for a bit of context and understanding
如果当前行不包含 bList @ index k 中的任何内容,则 k 将永远不会递增,因此将停留在该词处。
if currentline contains the thing in bList else if (myList.size() == bList.size())
不会被执行,你不想使用 break,那只是糟糕的风格。取而代之的是 while(... && myList.size() < bList.size()
)
另外,我不明白将同一行多次添加到 myList 背后的逻辑,您可能想遍历其他行。
bufferedReader.mark(0);
的目的是什么?
这里有一些代码可以满足您的需求:
LinkedList<String> found = new LinkedList<String>();
while ((currentLine = bufferedReader.readLine()) != null) {
foreach(String word : bList){
if(!found.contains(word) && currentline.contains(word)){
myList.add(currentLine);
found.add(word);
break;
}
}
}
这会在每行第一次包含来自 bList 的单词时将其添加到 myList。
或者,如果您不再需要 bList,您可以从 bList 中删除该词。
我的代码基本上是这样的:
bufferedReader = new BufferedReader(new InputStreamReader(inputFile));
while ((currentLine = bufferedReader.readLine()) != null) {
Document document;
DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
document = dBuilder.parse(new InputSource(new StringReader(currentLine)));
document.getDocumentElement().normalize();
NodeList nList = document.getElementsByTagName("record");
int j = 0;
int k = 0;
while (j < lines) {
if (currentLine.contains(bList.get(k))) {
myList.add(currentLine);
bufferedReader.mark(0);
k++;
j++;
}
else if (myList.size() == bList.size()) {
break;
}
else {
j++;
}
}
我有一个 bufferedReader,它逐行浏览文件。我想要的是根据该行是否包含 bList 中存在的字符串,将单次出现的行添加到 myList。无论出于何种原因,当我调试时,尽管 k++ 递增,但 bList 不会移动到下一个索引。我也不确定在这里使用 bufferedReader.mark(0) 是否正确,我以前使用过 inputFile.getChannel().position(0);但我遇到了:
'Error: XML document structures must start and end within the same entity'
我在调试时注意到的另一个问题是,当 myList 和 bList 的大小相等时,while 循环不会中断。相反,myList 一直到 34 的大小,而 bList 的大小保持为 2。
EDIT: Added more lines for a bit of context and understanding
如果当前行不包含 bList @ index k 中的任何内容,则 k 将永远不会递增,因此将停留在该词处。
if currentline contains the thing in bList else if (myList.size() == bList.size())
不会被执行,你不想使用 break,那只是糟糕的风格。取而代之的是 while(... && myList.size() < bList.size()
)
另外,我不明白将同一行多次添加到 myList 背后的逻辑,您可能想遍历其他行。
bufferedReader.mark(0);
的目的是什么?
这里有一些代码可以满足您的需求:
LinkedList<String> found = new LinkedList<String>();
while ((currentLine = bufferedReader.readLine()) != null) {
foreach(String word : bList){
if(!found.contains(word) && currentline.contains(word)){
myList.add(currentLine);
found.add(word);
break;
}
}
}
这会在每行第一次包含来自 bList 的单词时将其添加到 myList。 或者,如果您不再需要 bList,您可以从 bList 中删除该词。