Подскажите пожалуйста, не могу добить задачу.
package com.javarush.task.task22.task2207;
import java.io.*;
import java.util.*;
/*
Обращенные слова
*/
public class Solution {
public static void main(String[] args) throws FileNotFoundException {
List<String> words = new ArrayList<>();
List<Pair> result = new LinkedList<>();
Scanner scanner = new Scanner(System.in);
String fileName = scanner.nextLine();
FileReader fileReader = new FileReader(new File(fileName));
try (BufferedReader reader = new BufferedReader(fileReader)) {
String strLong = "";
String str;
while ((str = reader.readLine()) != null) {
strLong += " " + str;
}
words = new ArrayList<>(Arrays.asList(strLong.split("\\s")));
reader.close();
fileReader.close();
} catch (IOException e) {
e.printStackTrace();
}
for (int i = 0; i < words.size(); i++) {
for (int j = i + 1; j < words.size(); j++) {
StringBuilder temp = new StringBuilder(words.get(j));
if (!words.get(i).equals("") && words.get(i).equals(temp.reverse().toString())) {
Pair tempPair = new Pair();
tempPair.first = words.get(i);
tempPair.second = words.get(j);
words.remove(i);
words.remove(i);
// i--;
j--;
result.add(tempPair);
}
}
}
for (Pair p: result) {
System.out.println(p);
}
}
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;
}
}
}