It prints the correct output but it fails at the last requirement. A little help please ? :D
Thank you!
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 void main(String[] args) throws IOException {
ArrayList<String> wordsList = new ArrayList<String>();
Map<String, String> map = new LinkedHashMap<>();
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
String filename = reader.readLine();
File file = new File(filename);
Scanner scan = new Scanner(file);
while((scan.hasNext()))
{
wordsList.add(scan.next());
}
for(int i = 0; i < wordsList.size() - 1; i++)
{
for(int j = i + 1; j < wordsList.size(); j++)
{
StringBuilder sb = new StringBuilder(wordsList.get(j));
if(wordsList.get(i).equals(sb.reverse().toString()))
{
map.put(wordsList.get(i), sb.reverse().toString());
}
}
}
Iterator<Map.Entry<String, String>> iterator = map.entrySet().iterator();
while(iterator.hasNext())
{
Map.Entry<String, String> pairMap = iterator.next();
Pair pair = new Pair();
pair.first = pairMap.getKey();
pair.second = pairMap.getValue();
result.add(pair);
}
}
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;
}
}
}
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 void main(String[] args) throws IOException {
ArrayList<String> wordsList = new ArrayList<String>();
Map<String, String> map = new LinkedHashMap<>();
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
String filename = reader.readLine();
File file = new File(filename);
Scanner scan = new Scanner(file);
while((scan.hasNext()))
{
wordsList.add(scan.next());
}
for(int i = 0; i < wordsList.size() - 1; i++)
{
for(int j = i + 1; j < wordsList.size(); j++)
{
StringBuilder sb = new StringBuilder(wordsList.get(j));
if(wordsList.get(i).equals(sb.reverse().toString()))
{
map.put(wordsList.get(i), sb.reverse().toString());
}
}
}
Iterator<Map.Entry<String, String>> iterator = map.entrySet().iterator();
while(iterator.hasNext())
{
Map.Entry<String, String> pairMap = iterator.next();
Pair pair = new Pair();
pair.first = pairMap.getKey();
pair.second = pairMap.getValue();
result.add(pair);
}
}
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;
}
}
}