- Ты баг видишь?
- Нет.
- А он есть.
С любовью ко всем сусликам планеты Земля.
Всё работает, как надо, последняя строка добавляется, требуемые операции добавляются как по нотам. Но где-то есть есть что-то не устраивающее нашего представителя искусственного интеллекта.
Я решал задачу, восстанавливая полный список строк.
package com.javarush.task.task19.task1916;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
/*
Отслеживаем изменения
*/
public class Solution {
public static List<LineItem> lines = new ArrayList<LineItem>();
public static void main(String[] args) throws IOException {
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
FileReader reader1 = new FileReader(reader.readLine());
FileReader reader2 = new FileReader(reader.readLine());
StringBuilder sb1 = new StringBuilder("");
StringBuilder sb2 = new StringBuilder("");
while (reader1.ready()) {
sb1.append((char) reader1.read());
}
while (reader2.ready()) {
sb2.append((char) reader2.read());
}
String string1 = sb1.toString();
String string2 = sb2.toString();
String[] file1 = string1.split("\r\n");
String[] file2 = string2.split("\r\n");
List<String> list1 = new ArrayList<>();
List<String> list2 = new ArrayList<>();
for (String h : file1) {
list1.add(h);
}
for (String h : file2) {
list2.add(h);
}
int loopLength = list1.size() + list2.size();
try {
for (int i = 0; i < loopLength; i++) {
while(true){
if (list2.size()-1<i) {
lines.add(new LineItem(Type.REMOVED, list1.get(i)));
throw new ArrayIndexOutOfBoundsException();
}
if (list1.size()-1<i){
lines.add(new LineItem(Type.ADDED, list2.get(i)));
throw new ArrayIndexOutOfBoundsException();
}
int a = list1.get(i).charAt(7);
int b = list2.get(i).charAt(7);
if (a == b) {
lines.add(new LineItem(Type.SAME, list1.get(i)));
break;
}
if (a < b) {
lines.add(new LineItem(Type.REMOVED, list1.get(i)));
list2.add(i, list1.get(i));
break;
}
if (a > b) {
lines.add(new LineItem(Type.ADDED, list2.get(i)));
list1.add(i, list2.get(i));
break;
}
}
}} catch (Exception e) {
for (LineItem li : lines) {
System.out.println(li.line + li.type);
}
}
reader.close();
reader1.close();
reader2.close();
}
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;
}
}
}