Hello,
I don't know what is wrong with my code. when I test it with data task, I get the right output but I can't validate this task. I need to understand what is going on. I got this message "Checks that the lines list has the wording ADDED for the required lines in the correct places.", when I try to validate my code.
Thanks for your help.
package fr.codegym.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;
/*
Suivi des modifications
*/
public class Solution {
public static List<LineItem> lines = new ArrayList<>();
public static void main(String[] args) throws IOException {
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
BufferedReader br = new BufferedReader(new FileReader(reader.readLine())),
br1 = new BufferedReader(new FileReader(reader.readLine()));
reader.close();
List<String> original = new ArrayList<>(), copie = new ArrayList<>();
while (br.ready() ) {
original.add(br.readLine());
}
while (br1.ready() ) {
copie.add(br1.readLine());
}
br.close();
br1.close();
int minLength = original.size() < copie.size() ? original.size() : copie.size();
int i, j;
for (i = 0, j = 0; i < minLength || j < minLength; ) {
if (original.get(i).equals(copie.get(j))) {
lines.add(new LineItem(Type.SAME, original.get(i)));
i++;
j++;
} else if (!original.get(i).equals(copie.get(j))) {
if (original.get(i + 1).equals(copie.get(j))) {
lines.add(new LineItem(Type.REMOVED, original.get(i++)));
}
if (original.get(i).equals(copie.get(j + 1))) {
lines.add(new LineItem(Type.ADDED, copie.get(j++)));
}
}
}
while(i != original.size()) {
lines.add(new LineItem(Type.REMOVED, original.get(i)));
i++;
}
while (j != copie.size()) {
lines.add(new LineItem(Type.ADDED, copie.get(j)));
j++;
}
for (LineItem li : lines)
System.out.println(li.type + " " + li.line);
}
public static enum Type {
ADDED, // Nouvelle ligne ajoutée
REMOVED, // Ligne supprimée
SAME // Pas de changement
}
public static class LineItem {
public Type type;
public String line;
public LineItem(Type type, String line) {
this.type = type;
this.line = line;
}
}
}