File contents:
rat tar tart a
a tot tot tot
level code is
CodeGym si level
Console output:
rat tar
a a
tot tot
level level
is si
What I did wrong?
In the main method, read the name of a file that contains words separated by spaces from the console.
In the text of the file, find all pairs of words that are mirror images of each other. Add them to result.
Use StringBuilder.
The file encoding is UTF-8.
Example of file contents
rat tar tart a
a tot tot tot
Output:
rat tar
a a
tot tot
Requirements:
The main method must read the filename from the keyboard.
The main method must use StringBuilder.
The Solution class must contain a nested Pair class with equals, hashCode, and toString methods. Don't delete or change these methods.
The Pair class must declare a constructor without parameters, i.e. a default constructor.
The result list must be filled with correct pairs according to the task conditions.
....
rat tar tart a
a tot tot tot
level code is
CodeGym si level
when I look at your code then I think this might be a problem
tar rat tar
The output I expect it to give is
rat tar
rat tar
but it should be just
rat tar
Have you tested my code? I changed the contents of the file to:
tar rat tar
As a result, I got:
rat tar
rat tar
I just noticed that the toString method outputs data in alphabetical order, but I haven't changed anything in my code.
Yes, I've tested your code. The problem is with that sample that you should have one result pair and not two. 'tar' and 'rat' gives one pair and then one 'tar' is left. That is not enough to build another pair.
The output order does not matter at all (as far as I remember)