Could you plz help me with this task!!! Code produces correct results, however can't pass validation.
Have no idea what is wrong with the code.
package com.codegym.task.task19.task1916;
import java.io.*;
import java.util.ArrayList;
import java.util.List;
/*
Tracking changes
*/
public class Solution {
//List to store merged lines from both files
public static List<LineItem> lines = new ArrayList<>();
public static void main(String[] args) throws IOException {
//read in 2 file names
BufferedReader reader=new BufferedReader(new InputStreamReader(System.in));
String file1="";//"C:\\Users\\k.shayakhmetov\\Desktop\\CodeGym\\CodeGymTasks\\CodeGymTasks\\2.JavaCore\\src\\com\\codegym\\task\\task19\\task1916\\file1";
String file2="";//""C:\\Users\\k.shayakhmetov\\Desktop\\CodeGym\\CodeGymTasks\\CodeGymTasks\\2.JavaCore\\src\\com\\codegym\\task\\task19\\task1916\\file2";
try {
file1= reader.readLine();
file2= reader.readLine();
reader.close();
}
catch (IOException e){
e.printStackTrace();
}
//ArrayList objects to store content of both files
ArrayList<String> file1Content=readFile(file1);
ArrayList<String> file2Content=readFile(file2);
int index1 = 0, index2=0;
String file1Line= null;
String file2Line=null;
while (index1<file1Content.size() && index2<file2Content.size()){
file1Line=file1Content.get(index1);
file2Line=file2Content.get(index2);
if (file2Line.equals(file1Line)) {
lines.add(new LineItem(Type.SAME, file2Line));
index1++; index2++;
}
else {
if (index1 + 1 < file1Content.size() && file2Line.equals(file1Content.get(index1 + 1))) {
lines.add(new LineItem(Type.REMOVED, file1Content.get(index1)));
index1++;
}
else {
lines.add(new LineItem(Type.ADDED, file2Line));
index2++;
}
}
}
if (file1Content.size()>file2Content.size()){
for (int i=index1++;i<file1Content.size();i++){
file1Line=file1Content.get(i);
lines.add(new LineItem(Type.REMOVED,file1Line));
}
}
if (file2Content.size()>file1Content.size()){
for (int i=index2++;i<file2Content.size();i++){
file2Line=file2Content.get(i);
lines.add(new LineItem(Type.ADDED,file2Line));
}
}
for(int i=0;i<lines.size();i++){
System.out.println(lines.get(i).type + " " + lines.get(i).line);
}
}
public static ArrayList<String> readFile(String fileName) throws IOException {
//ArrayList object to store file content
ArrayList<String> fileContent=new ArrayList<>();
//BufferedReader object to read the file content
BufferedReader reader=new BufferedReader(new FileReader(fileName));
while (reader.ready()){
fileContent.add(reader.readLine());
}
reader.close();//close BufferedReader object
//return the file content
return fileContent;
}
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;
}
}
}