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
null
Level 26
Orlando
  • 01.07.2020
  • 356views
  • 2comments

Can someone point out what makes it fail the final condition?

Question about the task Tracking changes
Java Core,  Level 9,  Lesson 11
Under discussion

Read 2 file names from the console: file1 and file2.
Both files contain text, but file2 is an updated version of file1. Some of the lines are still the same.
You need to create a merged version of the lines by writing them to the lines list.
The ADDED and REMOVED labels can't be used consecutively—they must always separated by the SAME label.
The example includes empty lines for clarity.
Neither the original file nor the updated file have empty lines!

Example 1:
original   updated    merged
file1:     file2:     Result: (lines)

line1      line1      SAME line1
line2                 REMOVED line2
line3      line3      SAME line3
line4                 REMOVED line4
line5      line5      SAME line5
           line0      ADDED line0
line1      line1      SAME line1
line2                 REMOVED line2
line3      line3      SAME line3
           line4      ADDED line4
line5      line5      SAME line5
line0                 REMOVED line0

Example 2:
original   updated    merged
file1:     file2:     Result: (lines)

line1      line1      SAME line1
           line0      ADDED line0

In the example, empty lines mean that the line is not in the specified file.

Requirements:
  • The Solution class must contain a LineItem class.
  • The Solution class must have an enum called Type.
  • The Solution class must have a public static List<LineItem> field called lines that is initialized immediately.
  • In the main(String[] args) method, the program must read file names from the console (use BufferedReader).
  • In the main(String[] args) method, the BufferedReader used for reading input from the console must be closed after use.
  • The program must read the contents of the first and second file (use FileReader).
  • The file input streams (FileReader) must be closed.
  • The lines list should contain the merged version of the lines from the files. Each line should start with the label ADDED, REMOVED, or SAME, depending on the action taken.
package com.codegym.task.task19.task1916; import java.io.*; import java.util.ArrayList; import java.util.List; /* Tracking changes */ public class Solution { public static List<LineItem> lines = new ArrayList<>(); public static void main(String[] args) throws IOException, NullPointerException { BufferedReader reader = new BufferedReader(new InputStreamReader(System.in)); String file1 = reader.readLine(); String file2 = reader.readLine(); reader.close(); BufferedReader BReader1 = new BufferedReader(new FileReader(file1)); BufferedReader BReader2 = new BufferedReader(new FileReader(file2)); String line1 = "", line2 = ""; LineItem item; try { while (true) { line1 = BReader1.readLine(); line2 = BReader2.readLine(); if (line1.equals(line2)) { item = new LineItem(Type.SAME, line1); lines.add(item); } else if (line1.length() == 0) { item = new LineItem(Type.ADDED, line2); lines.add(item); } else if (line2.length() == 0) { item = new LineItem(Type.REMOVED, line1); lines.add(item); } else break; } } catch (Exception e) {} BReader1.close(); BReader2.close(); for (LineItem i : lines) System.out.println(i.type + " " + i.line); } public static enum Type { ADDED, // New line added REMOVED, // Line deleted SAME // No change } public static class LineItem { public Type type; public String line; public LineItem(Type type, String line) { this.type = type; this.line = line; } } }
0
Comments (2)
  • Popular
  • New
  • Old
You must be signed in to leave a comment
Guadalupe Gagnon
Level 37 , Tampa, United States
2 July 2020, 14:05useful
This code assumes that lines will be blank. The conditions specifically state that there are no blank lines. Here is an example of two files and the result: File 1: Line 1. Line 2. Line 3. Line 5. File 2: Line 1. Line 3. Line 4. Line 5. Line 6. Result: Line 1 SAME Line 2 REMOVED Line 3 SAME Line 4: ADDED Line 5: SAME Line 6: ADDED You can put these values in two files and use them for testing purposes. The result should match. One restriction that the conditions also point out is that ADDED and REMOVED can not be used consecutively. So the results can not have anything like this: ADDED, ADDED or REMOVED, REMOVED or ADDED, REMOVED or REMOVED, ADDED If the code marks a line as ADDED or REMOVED the next entry must be SAME.
+1
null
Level 26 , Orlando, United States
2 July 2020, 20:43
Thanks for your help. I misunderstood the conditions.
0
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.