Почему?
package com.javarush.task.task22.task2207;
import java.io.*;
import java.util.*;
/*
Обращенные слова
*/
public class Solution {
public static List<Pair> result = new LinkedList<>();
public static void main(String[] args) throws IOException {
List<String> listWithLine = new ArrayList<>();
try (
BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(new Scanner(System.in).nextLine())));
) {
String lineForRead;
while ((lineForRead = br.readLine()) != null) {
String[] tempLineList = lineForRead.split(" ");
for (String tempLineFromArray : tempLineList) {
listWithLine.add(tempLineFromArray);
}
}
}
for(int i = 0; i < listWithLine.size(); i++){
for(int j = i+1; j < listWithLine.size(); j++){
if((new StringBuilder(listWithLine.get(i)).reverse().toString()).equals(listWithLine.get(j))){
Pair pair = new Pair();
pair.second = listWithLine.remove(j);
pair.first = listWithLine.remove(i);
result.add(pair);
j = j - 1;
}
}
}
}
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;
}
}
}