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());
}
}
Your problem can be solved simpler using a TreeMap
:
TreeMap<Integer, String>
and call it errors
. (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:
SortedMap<Integer, String> visibleErrors = errors.subMap(fromLine, toLine + 1);