CodeGym
Promotion
CodeGym University
Learning
Course
Tasks
Surveys & Quizzes
Games
Help
Schedule
Community
Users
Forum
Chat
Articles
Success stories
Activity
Reviews
Subscriptions
Light theme
Question
  • Reviews
  • About us
Start
Start learning
Start learning now
  • All questions
hidden #10625598
Level 23
  • 05.08.2020
  • 300views
  • 0comments

can anybody see my mistake here?

Question about the task Inverted words
Java Multithreading,  Level 2,  Lesson 9
Under discussion


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.
package com.codegym.task.task22.task2207; import java.util.LinkedList; import java.util.*; import java.io.*; /* 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)); ArrayList<String> normal = new ArrayList<>(); while(reader.ready()){ String[] strings = (reader.readLine().split("\\s+")); for(String s : strings){ normal.add(s); } } for(int i = 0; i < normal.size(); i++){ StringBuilder str = new StringBuilder(normal.get(i)); if(normal.get(i).equals(str.reverse())){ Pair p = new Pair(); p.first = normal.get(i); p.second = str.toString(); } } } 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; } } }
0
Comments
  • Popular
  • New
  • Old
You must be signed in to leave a comment
This page doesn't have any comments yet
Learn
  • Registration
  • Java Course
  • Help with Tasks
  • Pricing
  • Game Projects
  • Java Syntax
Community
  • Users
  • Articles
  • Forum
  • Chat
  • Success Stories
  • Activity
  • Affiliate Program
Company
  • About us
  • Contacts
  • Reviews
  • Press Room
  • CodeGym for EDU
  • FAQ
  • Support
CodeGym CodeGym is an online course for learning Java programming from scratch. This course is a perfect way to master Java for beginners. It contains 1200+ tasks with instant verification and an essential scope of Java fundamentals theory. To help you succeed in education, we’ve implemented a set of motivational features: quizzes, coding projects, content about efficient learning and Java developer’s career.
Follow us
Interface language
Programmers Are Made, Not Born © 2023 CodeGym
MastercardVisa
Programmers Are Made, Not Born © 2023 CodeGym
This website uses cookies to provide you with personalized service. By using this website, you agree to our use of cookies. If you require more details, please read our Terms and Policy.