Doesn't even get out of the for loop. I've never seen this before. All that's being done is two ArrayLists are being compared and iterated. Doesn't make any sense to me that the for loop doesn't break. Spent 2 days trying to figure this out. I really just want to move on. Please explain this to me!
package com.codegym.task.task19.task1916;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
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 IOException {
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
String fileName1 = reader.readLine();
String fileName2 = reader.readLine();
reader.close();
BufferedReader fileReader1 = new BufferedReader(new FileReader(fileName1));
BufferedReader fileReader2 = new BufferedReader(new FileReader(fileName2));
ArrayList<String> file1Lines = new ArrayList<>();
while(fileReader1.ready()) {
file1Lines.add(fileReader1.readLine()); // put file1 lines to 1st ArrayList
}
fileReader1.close();
ArrayList<String> file2Lines = new ArrayList<>();
while(fileReader2.ready()) {
file2Lines.add(fileReader2.readLine()); // put file2 lines to 2nd ArrayList
}
fileReader2.close();
for(int i = 0; i < file1Lines.size(); i++) {
// if file1 line equals file2 line, then add new LineItem SAME to lines ArrayList
// if file1 line equals next file1 line, then add new LineItem REMOVED to lines ArrayList
// if file2 line equals file1 line, then add new LineItem ADDED to lines ArrayList
for(int j = 0; j < file2Lines.size();)
if(file1Lines.get(i).equals(file2Lines.get(j))) {
lines.add(new LineItem(Type.SAME, file1Lines.get(i)));
j++;
i++;
}
else if(file1Lines.get(i).equals(file1Lines.get(i+1))) {
lines.add(new LineItem(Type.REMOVED, file1Lines.get(i)));
i++;
}
else if(file2Lines.get(j).equals(file1Lines.get(i))) {
lines.add(new LineItem(Type.ADDED, file1Lines.get(i)));
j++;
}
}
}
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;
}
}
}