The meat of the program starts at line 51.
When I test the output, it looks like it is doing the right thing. This one is twisting my brain.
Any hits will be appreciated.
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) {
//reads two file names from the console
String file1 = null;
String file2 = null;
try (BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in))) {
file1 = bufferedReader.readLine();
file2 = bufferedReader.readLine();
} catch (IOException e) {
e.printStackTrace();
}
//reads content from each file
FileReader fileReader1 = null;
FileReader fileReader2 = null;
BufferedReader bufferedReader1 = null;
BufferedReader bufferedReader2 = null;
try {
fileReader1 = new FileReader(file1);
fileReader2 = new FileReader(file2);
bufferedReader1 = new BufferedReader(fileReader1);
bufferedReader2 = new BufferedReader(fileReader2);
//reads all the contents of both files to lists
List<String> linesFromOriginal = new ArrayList<>();
List<String> linesFromUpdated = new ArrayList<>();
while (bufferedReader1.ready()) {
linesFromOriginal.add(bufferedReader1.readLine());
}
while (bufferedReader2.ready()) {
linesFromUpdated.add(bufferedReader2.readLine());
}
//determines if each line from the file matches and adds entries to lines list
int originalIndex = 0;
int updatedIndex = 0;
while ((updatedIndex < linesFromUpdated.size()) && (originalIndex < linesFromOriginal.size())) {
String originalLine = linesFromOriginal.get(originalIndex);
String updatedLine = linesFromUpdated.get(updatedIndex);
if (updatedLine.equals(originalLine)) {
lines.add(new LineItem(Type.SAME, updatedLine));
originalIndex++;
updatedIndex++;
} else if ((originalIndex + 1 < linesFromOriginal.size()) && (updatedLine.equals(linesFromOriginal.get(originalIndex + 1)))) {
lines.add(new LineItem(Type.REMOVED, originalLine));
originalIndex++;
} else {
lines.add(new LineItem(Type.ADDED, updatedLine));
updatedIndex++;
}
}
// //****************for testing
// for (LineItem item : lines) {
// System.out.println(item.type + ": " + item.line);
// }
// //****************end testing
} catch (FileNotFoundException e) {
System.out.println("File not found.");
} catch (IOException e) {
e.printStackTrace();
} finally {
try { if (bufferedReader1 != null) bufferedReader1.close(); } catch (IOException e) { e.printStackTrace();}
try { if (bufferedReader2 != null) bufferedReader2.close(); } catch (IOException e) { e.printStackTrace();}
try { if (fileReader1 != null) fileReader1.close(); } catch (IOException e) { e.printStackTrace();}
try { if (fileReader2 != null) fileReader2.close(); } catch (IOException e) { e.printStackTrace();}
}
}
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;
}
}
}