This is my output....
a a
rat tar
tot tot..
Can someone guide me, what i am doing wrong
package com.codegym.task.task22.task2207;
import java.io.*;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.io.File;
import java.io.FileReader;
import java.io.InputStreamReader;
import java.util.*;
/*
Inverted words
*/
public class Solution {
public static List<Pair> result = new LinkedList<>();
public static void main(String args[]) throws Exception {
Map<String, String> hm
= new HashMap<String, String>();
StringBuilder sb = new StringBuilder();
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
String fileName = reader.readLine();
// File file = new File("C:\\Test\\pair.txt");
File file = new File(fileName);
BufferedReader bufferedReader = new BufferedReader(new FileReader(file));
ArrayList<String> wordList = new ArrayList<>();
String word;
while ((word = bufferedReader.readLine()) != null) {
String[] split = word.split(" ");
for (String s : split) {
wordList.add(s);
}
for (int i = 0; i < wordList.size(); i++) {
sb = new StringBuilder(wordList.get(i));//adding first word to stringbuilder
sb.reverse();
String x = sb.toString();
for (int j = i + 1; j < wordList.size(); j++) {
if (x.equals(wordList.get(j))) {
Pair pair = new Pair();
pair.first = wordList.get(i);
pair.second = wordList.get(j);
hm.put(wordList.get(i),wordList.get(j));
}
}
}
}
for (Map.Entry<String,String> mapEntry: hm.entrySet()){
result.add(new Pair(mapEntry.getKey(),mapEntry.getValue()));//adding the entries in the map to list
}
for (Pair pair : result){
System.out.print(pair.first + " " + pair.second + "\n");
}
}
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;
}
}
}