Hello,
I'm writing regarding task1916. I would love to hear at least some guides what is wrong with my solution. It seems to be working according to requirements but still getting error. I tried to ask on the forum, but even though my answer appears to be identical to those with success, I'm still failing. Not sure if you can see my code thus will paste it below ( actually this is someone else code, but I tried my identical code), contains only the main while loop, but I'm pretty sure that is enough. As I wrote any suggestions which allows me to make even little progress with this problem would be much appreciated :)
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++;
}
}
Regards.
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 {
//reads two file names from the console
String file1;
String file2;
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in));
file1 = bufferedReader.readLine();
file2 = bufferedReader.readLine();
bufferedReader.close();
String[] files = {file1, file2};
List<String> linesFromOriginal = new ArrayList<>();
List<String> linesFromUpdated = new ArrayList<>();
for (int i = 0; i < files.length; i++) {
BufferedReader br = new BufferedReader(new FileReader(files[i]));
while (br.ready()) {
if (i == 0) {
linesFromOriginal.add(br.readLine());
}
else
{
linesFromUpdated.add(br.readLine());
}
}
br.close();
}
//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
}
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;
}
}
}