简体   繁体   English

虽然循环似乎没有增加

[英]While loop doesn't seem to be incrementing

My code basically looks like this:我的代码基本上是这样的:


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++;
             }
         }

I have a bufferedReader which goes through a file line by line.我有一个 bufferedReader 逐行遍历文件。 What I want is for a single occurrence of line to be added to myList, based on whether that line contains a string which exists in bList.我想要的是根据该行是否包含 bList 中存在的字符串,将单次出现的行添加到 myList 中。 For whatever reason, when I debug, bList doesn't move to the next index despite the k++ incrementation.无论出于何种原因,当我调试时,尽管 k++ 递增,bList 都不会移动到下一个索引。 I'm also unsure if using bufferedReader.mark(0) is the right move here, I previously used inputFile.getChannel().position(0);我也不确定使用 bufferedReader.mark(0) 是否正确,我之前使用 inputFile.getChannel().position(0); but I was met with:但我遇到了:

'Error: XML document structures must start and end within the same entity' '错误:XML 文档结构必须在同一实体内开始和结束'

Another issue I noticed when debugging is that the while loop doesn't break when the size of myList and bList are equal.我在调试时注意到的另一个问题是,当 myList 和 bList 的大小相等时,while 循环不会中断。 Instead, myList makes it all the way to a size of 34 whilst bList remains at a size of 2.相反,myList 一直保持到 34 的大小,而 bList 保持在 2 的大小。

EDIT: Added more lines for a bit of context and understanding编辑:添加更多行以提供一些上下文和理解

k will never be incremented if currentline does not contain whatever is in bList @ index k and will therefore be stuck at this word.如果 currentline 不包含 bList @ index k 中的任何内容,k 将永远不会增加,因此将卡在这个单词上。

if currentline contains the thing in bList else if (myList.size() == bList.size()) won't be executed, and you don't want to use break, that is just bad style.如果 currentline 包含 bList 中的else if (myList.size() == bList.size())将不会被执行,并且您不想使用 break,那只是不好的风格。 Instead go for while(... && myList.size() < bList.size() )代替 go for while(... && myList.size() < bList.size() )

Also I don't understand the logic behind adding the same line to myList several times, you probably want to iterate over other lines.此外,我不明白多次将同一行添加到 myList 背后的逻辑,您可能想要迭代其他行。

What is the purpose of bufferedReader.mark(0); bufferedReader.mark(0);的目的是什么? ? ?

Here is some code that should do what you want:这是一些应该做你想做的代码:

  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;      
      }
    }
  }

this adds every line to myList the first time it contains a word from bList.这会将每一行添加到 myList 第一次包含来自 bList 的单词时。 alternatively you can delete the word from bList if you don't need bList anymore.或者,如果您不再需要 bList,您可以从 bList 中删除该单词。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM