I went through the code and I feel it is ok. I even added a check to ensure no duplicates are added to the list (lines 45-49).
Please ignore line 37.
Yet nothing is printed.
package com.codegym.task.task22.task2207;
import java.io.*;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Scanner;
/*
Inverted words
*/
public class Solution {
public static List<Pair> result = new LinkedList<>();
public static void main(String[] args) throws Exception{
//read the name of a file that contains words separated by spaces from the console.
//must use stringbuilder for the filereading
//the text of the file, find all pairs of words that are mirror images of each other. Add them to result.
BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
StringBuilder sb = new StringBuilder();
sb.append(bf.readLine());
bf.close();
File file = new File(sb.toString());
Scanner sc = new Scanner(file);
StringBuilder sb1 = new StringBuilder();
while (sc.hasNextLine())
sb1.append(sc.nextLine() + " ");
sc.close();
String[] splitWord = sb1.toString().trim().split(" ");
for (String word : splitWord) System.out.println(word);
for (int i = 0; i < splitWord.length; i++){
for (int j = 1; j < splitWord.length; j++){
StringBuilder wordJ = new StringBuilder();
StringBuilder wordI = new StringBuilder();
wordI.append(splitWord[i]);
wordJ.append(splitWord[j]);
if (i > 0) {
if ((wordI.equals(wordJ) || wordI.equals(wordJ.reverse()))
&& (splitWord[i-1].equals(splitWord[j-1]) || wordJ.equals(splitWord[i-1])))
continue;
}
if (wordI.equals(wordJ) || wordI.equals(wordJ.reverse()))
result.add(0, new Pair(splitWord[i], splitWord[j]));
}
}
for (Pair pair : result)
System.out.println(pair.toString());
}
public static class Pair {
String first;
String second;
public Pair(){}
public Pair(String first, String second){
this.first = first;
this.second = second;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Pair pair = (Pair) o;
if (first != null ? !first.equals(pair.first) : pair.first != null) return false;
return second != null ? second.equals(pair.second) : pair.second == null;
}
@Override
public int hashCode() {
int result = first != null ? first.hashCode() : 0;
result = 31 * result + (second != null ? second.hashCode() : 0);
return result;
}
@Override
public String toString() {
return first == null && second == null ? "" :
first == null ? second :
second == null ? first :
first.compareTo(second) < 0 ? first + " " + second : second + " " + first;
}
}
}