I just need help with the last invalidated condition.
"The joinData method must remove from the allLines list all lines in the linesForRemoval list if allLines contains all the lines in the linesForRemoval list."
The recommendation from the program:
"Be sure that the joinData() method deletes from the allLines list all lines in the linesForRemoval list."
But using println on allLines array after line 54 shows an empty array.
package com.codegym.task.task17.task1721;
import java.io.*;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
/*
Transactionality
Make the joinData method transactional, i.e. if it fails, then the data must not be changed.
1.X Read 2 file names from the console.
2.X Read from the files line by line. Read from the first file into allLines, from the second file — into linesForRemoval.
In the joinData method:
3. If the allLines list contains all the lines from linesForRemoval, then remove from the allLines list all the lines that are in linesForRemoval.
4. If the condition in Item 3 is not satisfied, then:
4.1. clear the data in allLines
4.2. throw a CorruptedDataException
The joinData method should be called in main. Handle all exceptions in the main method.
Don't forget to close the streams.
Requirements:
1.X The Solution class must contain a public static List<String> field called allLines.
2.X The Solution class must contain a public static List<String> field called linesForRemoval.
3.? The Solution class must have a public void joinData() method that can throw a CorruptedDataException.
4.X The program should read the names of two files from the console.
5.X The program must read line by line from the first file into the allLines list.
6.X The program must read line by line from the second file into the linesForRemoval list.
7.X The joinData method must remove from the allLines list all lines in the linesForRemoval list if allLines contains all the lines in the linesForRemoval list.
8.X The joinData method must clear the allLines list and throw a CorruptedDataException, if allLines does not contain all the lines in the linesForRemoval list.
9.X The joinData method should be called in main.
*/
public class Solution {
public static List<String> allLines = new ArrayList<>();
public static List<String> linesForRemoval = new ArrayList<>();
public static void main(String[] args) throws IOException {
String allLineFileName;
String linesForRemovalFileName;
BufferedReader rl = new BufferedReader(new InputStreamReader(System.in));
//read two file names
allLineFileName = rl.readLine();
linesForRemovalFileName = rl.readLine();
rl.close();
//read files into ArrayList
readInto(allLines, allLineFileName);
readInto(linesForRemoval, linesForRemovalFileName);
//run join data
Solution data = new Solution();
data.joinData();
}
public void joinData() throws CorruptedDataException {
for (Iterator<String> stringIterator = allLines.iterator();stringIterator.hasNext();){
String line1 = stringIterator.next();
for ( String line2: linesForRemoval){
if (line1.equals(line2)){
stringIterator.remove();
}
}
}
if (allLines.size() == 0) {
allLines.clear(); }
else {
allLines.clear();
throw new CorruptedDataException();
}
}
public static void readInto(List<String> target, String source){
try {
BufferedReader read = new BufferedReader(new FileReader(source));
String line;
while ((line = read.readLine()) != null){
target.add(line);
}
read.close();
}
catch (FileNotFoundException e) {
System.out.println("File not found!");
}
catch (IOException e) {
System.out.println("IO Error");
}
}
}