Could someone please explain to me what is wrong with my code????
package com.codegym.task.task22.task2207;
import java.io.*;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.*;
/*
Inverted words
*/
public class Solution {
public static List<Pair> result = new LinkedList<>();
public static void main(String[] args) throws IOException {
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
//We create a list containing all the lines of our file
List<String> list = readFileInList(reader.readLine());
Map<String,String> map1 = new HashMap<String, String>();
Map<String,String> map2 = new HashMap<String, String>();
StringBuilder sb = new StringBuilder();
for(String s: list){
sb.append(s);
}
String k = sb.toString();
//We create pairs of words by avoiding duplicates. For this we use maps
for(String l : list){
String[] tabl = l.split(" ");
for(String m : tabl){
String j = (new StringBuilder(m)).reverse().toString();
if(k.contains(j)){
if(m.equals(j))
map1.put(m,m);
else
map2.put(m,j);
}
}
}
//pairs are created from couples
for(Map.Entry<String,String> map : map1.entrySet()){
Pair pair = new Pair();
pair.first = map.getKey();
pair.second= map.getValue();
result.add(pair);
}
for(Map.Entry<String,String> mapy : map2.entrySet()){
Pair pair1 = new Pair();
pair1.first = mapy.getKey();
pair1.second = mapy.getValue();
result.add(pair1);
}
//Finally, the result is shown
for(Pair pair2 : result){
System.out.println(pair2);
}
}
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;
}
public Pair() {
}
@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;
}
}
//Reading the whole file in a List: Read all lines from a file. This method ensures
// that the file is closed when all bytes have been read or an I/O error, or other runtime exception,
// is thrown. Bytes from the file are decoded into characters using the specified charset.
//public static List readAllLines(Path path,Charset cs)throws IOException
/*\u000D followed by \u000A, CARRIAGE RETURN followed by LINE FEED*/
//u000A, LINE FEE
/*\u000D, CARRIAGE RETURN*/
public static List<String> readFileInList(String fileName)
{
List<String> lines = Collections.emptyList();
try
{
lines =
Files.readAllLines(Paths.get(fileName), StandardCharsets.UTF_8);
}
catch (IOException e)
{
// do something
e.printStackTrace();
}
return lines;
}
}