Hi there,
Could I have some help why my code is failing last condition?
Thanks
package com.codegym.task.task22.task2207;
import java.io.*;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
/*
Inverted words
*/
public class Solution {
public static List<Pair> result = new LinkedList<>();
public static void main(String[] args) {
try(BufferedReader br = new BufferedReader(new InputStreamReader(System.in))){
// Reads the name of the file
String fileName = br.readLine();
// Sets the file
File file = new File(fileName);
// Sets the input stream
FileInputStream fis = new FileInputStream(file);
// Array to store the bytes from the file
ArrayList<Byte> fisArray = new ArrayList<>();
// var to store the read byte
int thisChar;
// reads the stream while there's any byte left
while (fis.available() > 0){
thisChar = fis.read();
// if the current byte is not a part of the carriage return "\r\n"
// thisChar is added to the ArrayList
if(thisChar!='\r' && thisChar!='\n'){
fisArray.add((byte) thisChar);
}
}
// StringBuilder to make up the first word
StringBuilder first;
// StringBuilder to make up the second word
StringBuilder second;
// index to go through the ArrayList
int index = 0;
while(index < fisArray.size()){
first = new StringBuilder();
// Loop to make up the first word
while(index < fisArray.size() && (thisChar = fisArray.get(index))!=' '){
first.append((char) thisChar);
index++;
}
index++;
// Checks that the current word is not empty
if(first.toString().length()==0){
continue;
}
// Sets a new pair
Pair pair = new Pair();
pair.first = first.toString();
int n = index;
// Loop to build up the second word
while(n < fisArray.size()) {
second = new StringBuilder();
while (n < fisArray.size() && (thisChar = fisArray.get(n)) != ' ') {
second.append((char) thisChar);
n++;
}
n++;
// Checks if one word is the mirror of the other
if(pair.first.equals(second.reverse().toString())){
pair.second = second.reverse().toString();
// Ckecks that this pair is not yet in the result list
if(!result.contains(pair)){
result.add(pair);
break;
}
}
}
}
// Closes the stream
fis.close();
/*To check results
for(Pair pair : result){
System.out.println(pair.first + " - " + pair.second);
}
*/
} catch (IOException e) {
throw new RuntimeException(e);
}
}
public static class Pair {
String first;
String second;
public Pair() {
}
@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;
}
}
}