Uncomment lines 23, 60 and 61 to test the program and see.
Maybe you could find where I got it wrong.
Recommendation from the mentor was:
Be sure that the lines list has the REMOVED label with the required lines in the correct places.
package com.codegym.task.task19.task1916;
import java.io.*;
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 IOException {
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
String file1 = reader.readLine();
String file2 = reader.readLine();
reader.close();
BufferedReader fileReader1 = new BufferedReader(new FileReader(file1));
BufferedReader fileReader2 = new BufferedReader(new FileReader(file2));
//int i = 0;
while (true) {
String string1 = fileReader1.readLine();
String string2 = fileReader2.readLine();
//Break when both strings are null
if (string1 == null && string2 == null) break;
//READ THE COMMENT BELOW TO UNDERSTAND MY CODE. IT'S VERY SIMPLE!!!
//Each string can either be null or empty, so test for these two conditions.
//For SAME, only one test (condition) is needed: if string1 is equal to string2.
//For ADDED, two conditions must satisfy: string1 must either be empty or null.
//For REMOVED, two conditions must also satisfy: string2 must either be empty or null.
//Lastly, let the null conditions come before the empty conditions.
/*example: FOR REMOVED ---- string2 (second file)
string1 REMOVED (at this point, the computer considers string2 empty).
string1 REMOVED (but at this point, the computer considers string2 null).
This also applies to ADDED.
A string is considered null when it is empty and is the last line in the file.
*/
if (string1.equals(string2))
lines.add(new LineItem(Type.SAME, string1));
else if (string1 == null && !string2.isEmpty()) //Let the null condition come first: string1 is null -- ADDED
lines.add(new LineItem(Type.ADDED, string2));
else if (string1.isEmpty() && !string2.isEmpty()) //String1 is empty but string2 is not -- ADDED
lines.add(new LineItem(Type.ADDED, string2));
else if (!string1.isEmpty() && string2 == null) // Let the null condition come first: string2 is null -- REMOVED
lines.add(new LineItem(Type.REMOVED, string1));
else if (!string1.isEmpty() && string2.isEmpty()) //string2 is empty -- REMOVED
lines.add(new LineItem(Type.REMOVED, string1));
//This is to help view the output. It wasn't required in the task. Uncomment lines 23, 60 and 61
//System.out.println(string1 + " " + string2 + " --- " + lines.get(i).type + " " + lines.get(i).line);
//i++;
} //end while loop
fileReader1.close();
fileReader2.close();
} //close main
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 LineItem(Type type, String line) {
this.type = type;
this.line = line;
}
}
}