Валидатор при проверке задачи уходит в бесконечный цикл "Программа работала слишком долго и была закрыта!"
На Idea все в порядке
Строки выводятся согласно условию и примерам в условии.
От себя добавил проверку в случае пустых фалов 1 и 2
Помогите найти ошибку)
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.List;
import java.util.Scanner;
/*
Отслеживаем изменения
*/
public class Solution {
public static List<LineItem> lines = new ArrayList<LineItem>();
public static void main(String[] args) {
ArrayList<String> ar1 = new ArrayList<>();
ArrayList<String> ar2 = new ArrayList<>();
try (BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); Scanner scanner1 = new Scanner(new FileReader(br.readLine())); Scanner scanner2 = new Scanner(new FileReader( br.readLine()))) {
while (scanner1.hasNextLine()) {
ar1.add(scanner1.nextLine());
}
while (scanner2.hasNextLine()) {
ar2.add(scanner2.nextLine());
}
} catch (Exception e) {
e.printStackTrace();
}
// System.out.println(ar1);
// System.out.println(ar2);
if (ar1.size() == 1 && ar2.size() == 0)
lines.add(new LineItem(Type.REMOVED, ar1.get(0)));
if (ar2.size() == 1 && ar1.size() == 0)
lines.add(new LineItem(Type.ADDED, ar2.get(0)));
if (ar2.size() == 1) {
ar2.add(null);
}
if (ar1.size() == 1) {
ar1.add(null);
}
int i = 0;
while (!(i == ar1.size() - 1 || i == ar2.size() - 1 || ar1.size() == 0 || ar2.size() == 0)) {
if (ar1.get(i).equals(ar2.get(i))) {
lines.add(new LineItem(Type.SAME, ar2.get(i)));
i++;
continue;
}
if (ar1.get(i).equals(ar2.get(i + 1))) {
lines.add(new LineItem(Type.ADDED, ar2.get(i)));
ar2.remove(i);
continue;
}
if (ar1.get(i + 1).equals(ar2.get(i))) {
lines.add(new LineItem(Type.REMOVED, ar1.get(i)));
ar1.remove(i);
}
}
int stop = 0;
if (ar1.contains(null) && ar2.contains(null))
stop = 1;
ar1.remove(null);
ar2.remove(null);
if (!(ar1.size() == 0 || ar2.size() == 0) && stop == 0)
if (ar1.size() == ar2.size() || (ar1.size() > i && ar2.size() > i))//
if (ar1.get(i).equals(ar2.get(i))) {
lines.add(new LineItem(Type.SAME, ar1.get(i)));
i++;
}
if (!(ar1.size() == 0 || ar2.size() == 0) && stop == 0)
if (ar1.size() < ar2.size()) {
lines.add(new LineItem(Type.ADDED, ar2.get(i)));
ar2.remove(i);
if (ar1.size() > i && ar2.size() > i)//
if (ar1.get(i - 1).equals(ar2.get(i - 1))) {
lines.add(new LineItem(Type.SAME, ar1.get(ar1.size() - 1)));
i++;
}
if (ar1.size() < ar2.size()) {
lines.add(new LineItem(Type.ADDED, ar2.get(i)));
ar2.remove(i);
}
}
if (!(ar1.size() == 0 || ar2.size() == 0) && stop == 0)
if (ar1.size() > ar2.size()) {
lines.add(new LineItem(Type.REMOVED, ar1.get(i)));//
ar1.remove(i);//
if (ar1.size() > i && ar2.size() > i)//
if (ar1.get(i - 1).equals(ar2.get(i - 1))) {
lines.add(new LineItem(Type.SAME, ar2.get(ar2.size() - 1)));
i++;
}
if (ar1.size() > ar2.size()) {
lines.add(new LineItem(Type.REMOVED, ar1.get(i)));//
ar1.remove(i);
}
}
if (!(ar1.size() == 0 || ar2.size() == 0) && stop == 0)
if (ar1.size() < ar2.size()) {
lines.add(new LineItem(Type.ADDED, ar2.get(i)));
ar2.remove(i);
}
// System.out.println(ar1.get(ar1.size() - 1));
// System.out.println(ar2.get(ar2.size() - 1));
for (LineItem line : lines) {
System.out.println(line.type + " " + line.line);
}
// System.out.println("Size1 =" + ar1.size());
// System.out.println(ar1.toString());
// System.out.println("Size2 =" + ar2.size());
// System.out.println(ar2.toString());
}
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;
}
}
}
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.List;
import java.util.Scanner;
/*
Отслеживаем изменения
*/
public class Solution {
public static List<LineItem> lines = new ArrayList<LineItem>();
public static void main(String[] args) {
try {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String file1 = br.readLine();
String file2 = br.readLine();
br.close();
// String file1 = "c:\\file1.txt";
// String file2 = "c:\\file2.txt";
ArrayList<String> ar1 = new ArrayList<>();
FileReader reader1 = new FileReader(file1);
Scanner scanner1 = new Scanner(reader1);
ArrayList<String> ar2 = new ArrayList<>();
FileReader reader2 = new FileReader(file2);
Scanner scanner2 = new Scanner(reader2);
while (scanner1.hasNextLine())
{
ar1.add(scanner1.nextLine());
}
reader1.close();
scanner1.close();
while (scanner2.hasNextLine())
{
ar2.add(scanner2.nextLine());
}
reader2.close();
scanner2.close();
// System.out.println(ar1);
// System.out.println(ar2);
int i = 0;
while (!(i == ar1.size() || i == ar2.size() || ar1.size() == 0 || ar2.size() == 0)) {
if (ar1.get(i).equals(ar2.get(i))) {
lines.add(new LineItem(Type.SAME, ar2.get(i)));
i++;
continue;
}
if (ar1.get(i).equals(ar2.get(i + 1))) {
lines.add(new LineItem(Type.ADDED, ar2.get(i)));
ar2.remove(i);
continue;
}
if (ar1.get(i + 1).equals(ar2.get(i))) {
lines.add(new LineItem(Type.REMOVED, ar1.get(i)));
ar1.remove(i);
}
}
if (!(ar1.size() == 0 || ar2.size() == 0))
if (ar1.size() < ar2.size()) {
lines.add(new LineItem(Type.ADDED, ar2.get(ar2.size() - 1)));
} else {
lines.add(new LineItem(Type.REMOVED, ar1.get(ar1.size() - 1)));
}
for (LineItem line : lines) {
System.out.println(line.type + " " + line.line);
}
// System.out.println("Size1 =" + ar1.size());
// System.out.println(ar1.toString());
// System.out.println("Size2 =" + ar2.size());
// System.out.println(ar2.toString());
} catch (IOException e) {
e.printStackTrace();
}
}
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;
}
}
}