捕获以前的错误

发布于 2025-01-05 08:26:17 字数 1038 浏览 2 评论 0原文

我正在对文本文件进行自定义验证。第一次对整个文件进行验证。如果有错误,则显示在另一个 JTextArea 中。之后,仅对可见部分进行验证。我的问题是我无法存储以前的错误并与当前的错误结合起来显示。例如,第一次,我收到错误 第1行、第5行、第6行、第100行、第500行。从下次开始,可见部分将是1-50行,仅对1-50行进行验证,因此100和500的错误将消失。我尝试将之前的错误存储在临时变量中,下一个问题是,现在这些错误将变为仅适用于 1-50,有人可以告诉我该怎么做吗?

我尝试过的代码,

      private void displayError() {
      errorText.setText(null); 
      String err = null;
      try{
        //tempErrors
        if(!prevErrors.isEmpty()){
            for(int i=0; i< prevErrors.size(); i++){
                err = prevErrors.get(i).toString();
                if(!errors.contains(err)){
                    errors.add(prevErrors.get(i).toString());
                }
            }
        }
    }
    catch(Exception ex){
        System.out.println(ex);
    }        

    Iterator it = errors.iterator();
    while(it.hasNext()){
        errorText.append(it.next() + "\n");
    }

    prevErrors = new ArrayList();        
    for(int i=0; i< errors.size(); i++){
       prevErrors.add(errors.get(i).toString());
    }
}

I am doing a custom validation for text files. For the first time, validation is done for whole file. The errors if any, are shown in another JTextArea. After that validation is done to only those part which is visible. My problem is i am not able to store the previous errors and combine with the current errors to show. For example, First time, i get errors in
line 1, line 5, line 6, line 100, line 500. from next time the visible part will be 1-50 lines, the validation will be done only for 1-50, so the error of 100 and 500 going off. I tried it by storing the previous errors in a temp variable, the next problem is, Now the privious errors will become only for 1-50, Can someone tell me how to go about it?.

The code i tried,

      private void displayError() {
      errorText.setText(null); 
      String err = null;
      try{
        //tempErrors
        if(!prevErrors.isEmpty()){
            for(int i=0; i< prevErrors.size(); i++){
                err = prevErrors.get(i).toString();
                if(!errors.contains(err)){
                    errors.add(prevErrors.get(i).toString());
                }
            }
        }
    }
    catch(Exception ex){
        System.out.println(ex);
    }        

    Iterator it = errors.iterator();
    while(it.hasNext()){
        errorText.append(it.next() + "\n");
    }

    prevErrors = new ArrayList();        
    for(int i=0; i< errors.size(); i++){
       prevErrors.add(errors.get(i).toString());
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

哆兒滾 2025-01-12 08:26:17

使用 TreeMap< 可以更简单地解决您的问题/code>

  1. 创建一个 树形图<整数, String> 并将其称为错误。 (键是行号,值是错误信息。)
  2. 解析文件一次并用找到的错误(和行)填充 errors-map

当文件的一部分可见时,您可以使用子地图获取该部分内错误的方法:

SortedMap<Integer, String> visibleErrors = errors.subMap(fromLine, toLine + 1); 

Your problem can be solved simpler using a TreeMap:

  1. Create an TreeMap<Integer, String> and call it errors. (The key is line number and the value is the error information.)
  2. Parse the file once and fill the errors-map with the found errors (and lines)

When a part of the file is visible you use the subMap method to get the errors within that part:

SortedMap<Integer, String> visibleErrors = errors.subMap(fromLine, toLine + 1); 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文