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, NullPointerException {
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
String file1 = reader.readLine();
String file2 = reader.readLine();
reader.close();
BufferedReader BReader1 = new BufferedReader(new FileReader(file1));
BufferedReader BReader2 = new BufferedReader(new FileReader(file2));
String line1 = "", line2 = "";
LineItem item;
try {
while (true) {
line1 = BReader1.readLine();
line2 = BReader2.readLine();
if (line1.equals(line2)) {
item = new LineItem(Type.SAME, line1);
lines.add(item);
} else if (line1.length() == 0) {
item = new LineItem(Type.ADDED, line2);
lines.add(item);
} else if (line2.length() == 0) {
item = new LineItem(Type.REMOVED, line1);
lines.add(item);
}
else break;
}
} catch (Exception e) {}
BReader1.close();
BReader2.close();
for (LineItem i : lines)
System.out.println(i.type + " " + i.line);
}
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;
}
}
}
Can someone point out what makes it fail the final condition?
Under discussion
Comments (2)
- Popular
- New
- Old
You must be signed in to leave a comment
Guadalupe Gagnon
2 July 2020, 14:05useful
This code assumes that lines will be blank. The conditions specifically state that there are no blank lines.
Here is an example of two files and the result:
File 1:
Line 1.
Line 2.
Line 3.
Line 5.
File 2:
Line 1.
Line 3.
Line 4.
Line 5.
Line 6.
Result:
Line 1 SAME
Line 2 REMOVED
Line 3 SAME
Line 4: ADDED
Line 5: SAME
Line 6: ADDED
You can put these values in two files and use them for testing purposes. The result should match. One restriction that the conditions also point out is that ADDED and REMOVED can not be used consecutively. So the results can not have anything like this:
ADDED, ADDED
or
REMOVED, REMOVED
or
ADDED, REMOVED
or
REMOVED, ADDED
If the code marks a line as ADDED or REMOVED the next entry must be SAME.
+1
null
2 July 2020, 20:43
Thanks for your help. I misunderstood the conditions.
0