пока написал код для случая если количество строк в двух файлах одинаковое
Подскажите, пожалуйста, как выполнить верное условие для тех строк, которые имеются в двух файлах
тот пример, что нам дан выдает у меня:
SAME строка 1
REMOVED строка 2
SAME строка 3
ADDED
SAME строка 5
ADDED строка 0
SAME строка 1
REMOVED строка 2
SAME строка 3
ADDED строка 4
SAME строка 5
ADDED
второй пример выдает верный результатpackage com.javarush.task.task19.task1916;
import java.io.*;
import java.util.ArrayList;
import java.util.List;
/*
Отслеживаем изменения
*/
public class Solution {
public static List<LineItem> lines = new ArrayList<LineItem>();
public static void main(String[] args) {
try(BufferedReader nameReader = new BufferedReader(new InputStreamReader(System.in));
BufferedReader firstReader = new BufferedReader(new FileReader(nameReader.readLine()));
BufferedReader secondReader = new BufferedReader(new FileReader(nameReader.readLine()))) {
List<String> firstLines = new ArrayList<>();
String s1;
while ((s1 = firstReader.readLine()) != null) {
firstLines.add(s1);
}
List<String> secondLines = new ArrayList<>();
String s2;
while ((s2 = secondReader.readLine()) != null) {
secondLines.add(s2);
}
int linesCount = -1;
int nextFirstCount = 0;
int nextSecondCount = 0;
int endFirstCount = firstLines.size();
int endSecondCount = secondLines.size();
while (true) {
if(nextFirstCount == endFirstCount || nextSecondCount == endSecondCount){
break;
}
if (firstLines.get(nextFirstCount).equals(secondLines.get(nextSecondCount))) {
lines.add(new LineItem(Type.SAME, firstLines.get(nextFirstCount)));
}
else if(!(lines.get(linesCount).type == Type.ADDED) || !(lines.get(linesCount).type == Type.REMOVED)){
if((!firstLines.contains(secondLines.get(nextSecondCount)) || secondLines.contains(firstLines.get(nextFirstCount)))){
lines.add(new LineItem(Type.ADDED, secondLines.get(nextSecondCount)));
}
else if (!secondLines.contains(firstLines.get(nextFirstCount))) {
lines.add(new LineItem(Type.REMOVED, firstLines.get(nextFirstCount)));
}
}
nextFirstCount++;
nextSecondCount++;
linesCount++;
}
//проверка
for(LineItem s : lines){
System.out.println(s.type + " " + s.line);
}
}
catch (IOException e){
}
}
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;
}
}
}




