The goal of my program is to compare two text files and print the uncommon sub-sequences of one of the text files. When I define two string arrays such as
String [] s1 = {"b","a","b","a","b"};
String [] s2 = {"b","a","b","b","a"};
and compare them, it's working. (It prints: [b a b a b , b a a b , b b a b , a b a b , a a b ]) However it doesn't work with text files. What's wrong with the program? I don't get any output and the program does not terminate, probably due to an infinite loop.
public class hess {
static ArrayList<String> arr1 = new ArrayList<String>();
static ArrayList<String> arr2 = new ArrayList<String>();
public void printAllSubSequences(String[] arrInput) {
int[] temp = new int[arrInput.length];
int index = 0;
solve(arrInput, index, temp);
}
private void solve(String[] arrInput, int index, int[] temp) {
if (index == arrInput.length) {
print(arrInput, temp);
return;
}
// set the current index bit and solve it recursively
temp[index] = 1;
solve(arrInput, index + 1, temp);
// unset the current index bit and solve it recursively
temp[index] = 0;
solve(arrInput, index + 1, temp);
}
private void print(String[] arrInput, int[] temp) {
String result = "";
for (int i = 0; i < temp.length; i++) {
if (temp[i] == 1)
result += arrInput[i] + " ";
}
arr1.add(result);
}
public void printAllSubSequences1(String[] arrInput) {
int[] temp = new int[arrInput.length];
int index = 0;
solve1(arrInput, index, temp);
}
private void solve1(String[] arrInput, int index, int[] temp) {
if (index == arrInput.length) {
print1(arrInput, temp);
return;
}
// set the current index bit and solve it recursively
temp[index] = 1;
solve1(arrInput, index + 1, temp);
// unset the current index bit and solve it recursively
temp[index] = 0;
solve1(arrInput, index + 1, temp);
}
private void print1(String[] arrInput, int[] temp) {
String result = "";
for (int i = 0; i < temp.length; i++) {
if (temp[i] == 1)
result += arrInput[i] + " ";
}
arr2.add(result);
}
public static void main(String[] args) throws IOException {
String[] s1 = read("C:\\Users\\fener\\Desktop\\producerconsumer\\1575229506551OutBug.txt");
String[] s2 = read("C:\\Users\\ahmet\\Desktop\\bitirme\\producerconsumer\\1575371318152Out.txt");
new hess().printAllSubSequences(s1);
new hess().printAllSubSequences1(s2);
System.out.println(arr1);
System.out.println(arr2);
arr1.removeAll(arr2);
System.out.println(arr1);
}
private static String[] read(String location) throws IOException {
BufferedReader reader1 = new BufferedReader(new FileReader(location));
String line;
ArrayList<String> lines = new ArrayList<String>();
while ((line = reader1.readLine()) != null) {
lines.add(line);
}
String[] result = new String[lines.size()];
for(int i=0; i<lines.size(); i++) {
result[i] = lines.get(i);
}
return result;
}
}
'''