The loop isn't adding a to the result list, not sure why.
package com.codegym.task.task22.task2207;
import java.io.*;
import java.util.*;
/*
Inverted words
*/
public class Solution {
public static List<Pair> result = new LinkedList<>();
public static ArrayList<String> words = new ArrayList<>();
public static ArrayList<String> wordsWithReverses = new ArrayList<>();
public static void main(String[] args) throws IOException {
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
String fileName = reader.readLine();
FileReader fileReader = new FileReader(fileName);
//Read the file and add all the chars between spaces into words ArrayList
int content;
StringBuilder builder = new StringBuilder();
while ((content = fileReader.read()) != - 1){
if(content != 32){
String str = Character.toString((char)content);
//System.out.println(str);
builder.append(str);
}
else {
words.add(builder.toString());
builder.setLength(0);
}
}
/*Check if the ArrayList words contains the mirrored version of the iterated element, if so, create a Pair object,
add the unmirrored and mirrored words to the fields first and second, add the pair to the result listd
*/
for(String element: words){
StringBuilder reversedWord = new StringBuilder(element);
reversedWord = reversedWord.reverse();
if(wordsWithReverses.contains(reversedWord.toString())){
wordsWithReverses.remove(reversedWord.toString());
Pair pair = new Pair();
pair.first = element;
pair.second = reversedWord.toString();
result.add(pair);
}
else {
wordsWithReverses.add(element);
}
}
/*For testing purposes
System.out.println("test");
if(result.isEmpty()){
System.out.println("Result is empty");
}
if(words.isEmpty()){
System.out.println("words is empty");
}
*/
for(Pair element: result){
System.out.println(element.first+" "+element.second);
}
//System.out.println(result.get(1).first);
//System.out.println(result.get(1).second);
/*for(String wordElement: words){
System.out.println(wordElement);
}
*/
}
public static class Pair {
String first;
String 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;
}
}
}