Подскажите пожалуйста, что тут не так? Результаты с разными тестовыми значениями соответствуют ожидаемым, но при этом валидатор ругается.
int max = 0;
if (lines1.size() > lines2.size()) {
max = lines1.size();
} else max = lines2.size();
for (int i = 0; i < max; i++) {
try {
lines1.get(i);
} catch (IndexOutOfBoundsException e) {
lines.add(new LineItem(Type.ADDED, lines2.get(i)));
continue;
}
try {
lines2.get(i);
} catch (IndexOutOfBoundsException e) {
lines.add(new LineItem(Type.REMOVED, lines1.get(i)));
continue;
}
if ((lines1.get(i)).equals(lines2.get(i))) {
lines.add(new LineItem(Type.SAME, lines2.get(i)));
} else if (!lines2.get(i).equals("") && lines2.get(i) != null && !lines2.get(i).equals(lines1.get(i))) {
lines.add(new LineItem(Type.ADDED, lines2.get(i)));
} else if ((lines2.get(i).equals("") || lines2.get(i) == null) && !lines2.get(i).equals(lines1.get(i))) {
lines.add(new LineItem(Type.REMOVED, lines1.get(i)));
}
}
package com.javarush.task.task19.task1916;
import java.io.*;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
/*
Отслеживаем изменения
*/
public class Solution {
public static List<LineItem> lines = new ArrayList<LineItem>();
public static void main(String[] args) throws IOException {
List<String> lines1 = new ArrayList<String>();
List<String> lines2 = new ArrayList<String>();
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
BufferedReader inputStream1 = new BufferedReader(new FileReader(br.readLine()));
// BufferedReader inputStream1 = new BufferedReader(new FileReader("C:\\Users\\yanagibitipogib\\Desktop\\2.txt"));
while (inputStream1.ready()) {
lines1.add(inputStream1.readLine().replace("\uFEFF", ""));
}
inputStream1.close();
BufferedReader inputStream2 = new BufferedReader(new FileReader(br.readLine()));
//BufferedReader inputStream2 = new BufferedReader(new FileReader("C:\\Users\\yanagibitipogib\\Desktop\\5.txt"));
br.close();
while (inputStream2.ready()) {
lines2.add(inputStream2.readLine().replace("\uFEFF", ""));
}
inputStream2.close();
//C:\Users\yanagibitipogib\Desktop\1.txt
// System.out.println(lines2.get(0).equals(lines1.get(0)));
// System.out.println((lines2.get(0)) + (lines1.get(0)));
// System.out.println((lines2.get(1)) + (lines1.get(1)));
int max = 0;
if (lines1.size() > lines2.size()) {
max = lines1.size();
} else max = lines2.size();
for (int i = 0; i < max; i++) {
try {
lines1.get(i);
} catch (IndexOutOfBoundsException e) {
lines.add(new LineItem(Type.ADDED, lines2.get(i)));
continue;
}
try {
lines2.get(i);
} catch (IndexOutOfBoundsException e) {
lines.add(new LineItem(Type.REMOVED, lines1.get(i)));
continue;
}
if ((lines1.get(i)).equals(lines2.get(i))) {
lines.add(new LineItem(Type.SAME, lines2.get(i)));
} else if (!lines2.get(i).equals("") && lines2.get(i) != null && !lines2.get(i).equals(lines1.get(i))) {
lines.add(new LineItem(Type.ADDED, lines2.get(i)));
} else if ((lines2.get(i).equals("") || lines2.get(i) == null) && !lines2.get(i).equals(lines1.get(i))) {//(!lines1.get(i).equals(lines2.get(i)))
lines.add(new LineItem(Type.REMOVED, lines1.get(i)));
}
}
// for (LineItem s : lines) {
// System.out.println(s.type + " " + s.line);
// }
}
public static enum Type {
ADDED, //добавлена новая строка
REMOVED, //удалена строка
SAME //без изменений
}
public static class LineItem {
public Type type;
public String line;
public LineItem(Type type, String line) {
this.type = type;
this.line = line;
}
}
}