捕获以前的错误
我正在对文本文件进行自定义验证。第一次对整个文件进行验证。如果有错误,则显示在另一个 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
使用
TreeMap< 可以更简单地解决您的问题/code>
:
树形图<整数, String>
并将其称为错误
。 (键是行号,值是错误信息。)errors
-map当文件的一部分可见时,您可以使用
子地图
获取该部分内错误的方法:Your problem can be solved simpler using a
TreeMap
:TreeMap<Integer, String>
and call iterrors
. (The key is line number and the value is the error information.)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: