Is it because in case the files have more lines than 6 it won't work?
if (counterFile1 == 6){
while (true) {
if (LinesFile1.get(counterFile1).equals(LinesFile2.get(counterFile2))) {
lines.add(new LineItem(Type.SAME, LinesFile2.get(counterFile2)));
counterFile2++;
} else {
lines.add(new LineItem(Type.ADDED, LinesFile2.get(counterFile2)));
counterFile2++;
if (counterFile1 == LinesFile1.size()) break;
}
if ( counterFile2 == LinesFile2.size()) break;
}
}
package com.codegym.task.task19.task1916;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
/*
Tracking changes
*/
public class Solution {
public static List<LineItem> lines = new ArrayList<>();
public static void main(String[] args) throws Exception {
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in));
String file1 = bufferedReader.readLine();
String file2 = bufferedReader.readLine();
bufferedReader.close();
BufferedReader bufferedReader1 = new BufferedReader(new FileReader(file1));
BufferedReader bufferedReader2 = new BufferedReader(new FileReader(file2));
/*
I was over thinking it and the key was the restriction that "The ADDED and REMOVED labels can't be used consecutively".
This means that when you either ADDED or REMOVED a line (and there are still lines to be parsed),
then the next one has to be SAME.
Basically you have to test each line of the file and if they are the same:
lines.add(new LineItem(Type.SAME, theFullLine));
and move your counter for each by 1
if they are different check to see if the next line from the original matches, if true:
lines.add(new LineItem(Type.REMOVED, theFullLine));
and move only the counter from file 1
and if false
lines.add(new LineItem(Type.ADDED, theFullLine));
and move only the counter from the second file
*/
List<String> LinesFile1 = new ArrayList<>();
List<String> LinesFile2 = new ArrayList<>();
while (true){
LinesFile1.add(bufferedReader1.readLine());
LinesFile2.add(bufferedReader2.readLine());
if ((!(bufferedReader1.ready()) && (!bufferedReader2.ready()))) break;
}
bufferedReader1.close();
bufferedReader2.close();
int counterFile1 = 0;
int counterFile2 = 0;
String lineFile1;
String lineFile2;
for (int i = 0; i < LinesFile2.size(); i++){ //6,6,5
lineFile1 = LinesFile1.get(counterFile1);
lineFile2 = LinesFile2.get(counterFile2);
if (lineFile1.equals(lineFile2))
{
lines.add(new LineItem(Type.SAME, lineFile1));
counterFile1++;
counterFile2++;
}
else {
lineFile1 = LinesFile1.get(counterFile1+1);
if (lineFile1.equals(lineFile2)) {
lines.add(new LineItem(Type.REMOVED, LinesFile1.get(i)));
counterFile1++;
}
else {
lines.add(new LineItem(Type.ADDED, lineFile2));
counterFile2++;
}
}
if (counterFile1 == 6){
while (true) {
if (LinesFile1.get(counterFile1).equals(LinesFile2.get(counterFile2))) {
lines.add(new LineItem(Type.SAME, LinesFile2.get(counterFile2)));
counterFile2++;
} else {
lines.add(new LineItem(Type.ADDED, LinesFile2.get(counterFile2)));
counterFile2++;
if (counterFile1 == LinesFile1.size()) break;
}
if ( counterFile2 == LinesFile2.size()) break;
}
}
} //main bracket
for (LineItem line : lines)
System.out.println(line.toString());
System.out.println(counterFile1);
System.out.println(counterFile2);
}
public static enum Type {
ADDED, // New line added
REMOVED, // Line deleted
SAME // No change
}
public static class LineItem {
public Type type;
public String line;
public String toString() {
return String.format("LineItem{%s - %s}", type, line);
}
public LineItem(Type type, String line) {
this.type = type;
this.line = line;
}
}
}