Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

I need help to rewrite below Java code to write into txt file all records belonging to src2. package FileProcessing; import java.io.BufferedReader; import java.io.BufferedWriter; import

I need help to rewrite below Java code to write into txt file all records belonging to src2.

package FileProcessing;

import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.FileReader; import java.io.FileWriter; import java.io.IOException;

public class FileProcessing {

public static void main(String[] args) { // Input and output file paths String fi = "Sample_file.txt"; String fo = fi + ".data.txt"; // Counter for eligible transaction records int rec_cnt = 0; // Flag indicating if the current transaction record is eligible boolean eligible_flag = true;

try (BufferedReader f1 = new BufferedReader(new FileReader(fi)); BufferedWriter f2 = new BufferedWriter(new FileWriter(fo, true))) { String ftn_recs; // Read each line from input file while ((ftn_recs = f1.readLine()) != null) { // Check if the transaction record is eligible rec_cnt = checkEligibleTrnRec(ftn_recs, eligible_flag, rec_cnt); // If the record is eligible, write it to the output file if (eligible_flag) { f2.write(ftn_recs + " "); } } // Write the total count of eligible transaction records to the output file String write_trn_rec_cnt = "Total Count : " + rec_cnt + " "; f2.write(write_trn_rec_cnt); } catch (IOException e) { e.printStackTrace(); } }

// Method to check if a transaction record is eligible public static int checkEligibleTrnRec(String chk_rec, boolean is_eligible, int rec_cnt2) { // Split the transaction record by spaces String[] chk_rec_sep = chk_rec.split("\\s+"); // Counter for eligible transaction records int rec_cnt = rec_cnt2; // Array of source systems that indicate if a transaction record is eligible or not String[] src1 = {"Y28", "D69", "NYB", "WKD"}; String[] src2 = {"AES", "CCX", "CIF", "MSB", "PTT", "SEI", "TD1", "TD2", "TD3", "TSP", "TSX", "PAP", "USR"};

// Loop through the source systems in the first array for (String src_j : src1) { // If the record is not eligible or contains only whitespace characters // OR If the source system occurs more than once and its code is in the third position // System.out.println(chk_rec_sep[2]); if ((!is_eligible && chk_rec.isEmpty()) || (countOccurrences(chk_rec_sep, src_j) > 1 && chk_rec_sep[2].equals(src_j))) { // Set the eligibility flag to false and exit the loop is_eligible = false; break; } }

// Loop through the source systems in the second array for (String src_i : src2) { // If the record is eligible and contains only whitespace characters if (is_eligible && chk_rec.isEmpty()) { // Exit the loop break; // If the source system occurs more than once and its code is in the third position } else if (countOccurrences(chk_rec_sep, src_i) > 1 && chk_rec_sep[2].equals(src_i)) { // Set the eligibility flag to true, increment the counter, and exit the loop is_eligible = true; rec_cnt++; break; } }

return rec_cnt; }

public static int countOccurrences(String[] arr, String val) { int count = 0; for (String s : arr) { if (s.equals(val)) { count++; } } return count; } }

Below is the sample file with records for CCX. I only need all information related to src2 to be printed out into new text file. Please create extra records for different src1 and src2 and tested it out before giving solution to me.

Date: 23 SEP 20 Time: 8:40 Transaction Report

TRANSACTION NUMBER AMOUNT CNCY SVC DISPOSITION DATE/TIME % PT FT USER LOC ------------------------------------------------------------------------------------------------------------------------------------ F01120707112912345 .000 CCX AUTO HOLD 07 DEC 24 09:11:32 89.0 85.0 101.0 SYSTEM CCX

F01120707112912345 - Transaction Details:

Format type : U Request type : S Criteria Set : Transaction Location : CCX Rule Based Scanning : Applied ------------------------------------------------------------------------------------------------------------------------------------ Free Form: [ID]TEST01ADAM 1* 2* 3* 4* 5* 6* 7* 8* [FULL_NAME]ESAH, WALI ADAM [ADDRESS] [ADDRESS2] [CITY_STATE] [PROVINCE] [COUNTRY] [DATE_OF_BIRTH] [ID1]||TEST01ADAM [LINE]1

F01120707112912345 - Match 1 [Field Based Scanning Risk Entity Match] ------------------------------------------------------------------------------------------------------------------------------------ * Name : ESAH, WALI ADAM Address : LAHORE CITY City/State : LAHORE DISTRICT Country : PK Origin : ACCUITY Risk Entity Comments : Title : MAULANA Remarks : ALVI, MOHAMMAD MASOOD AZHAR Risk Entity Info : 407910 Additional information 1 : 07/10/1968 03 Additional information 2 : SDGT Additional information 3 : 11/04/2010 05/30/2019 Additional information 4 : Additional information 5 : PAKISTAN PUNJAB PROVINCE Additional information 6 : 407904 Additional information 7 : Additional information 8 : PAKISTAN Additional information 9 : PAKISTAN Additional information 10 : STRONG Free Form Match Text : ESAH, WALI ADAM ------------------------------------------------------------------------------------------------------------------------------------

I want the new text file to be as below. Exclude all transactions that aren't in src2 "AES", "CCX", "CIF", "MSB", "PTT", "SEI", "TD1", "TD2", "TD3", "TSP", "TSX", "PAP", "USR". Please refer to the attached screenshot below sample output file for the reference on what to expect in new file. Transaction information related to src2 until it reaches the end of transaction for that particular src2.

image text in transcribed

Fe112a7a7112912345 - Natch 1 [Field Based Scanning Risk Entity Natch] F21129797112912345 .699 CIF AUTO HOLO 67 DEC 24 e9:11:32 89.e 85.0 1e1.e SYSTEM CIF F211297a7112912345 - Transaction Details: Free farm: [FULL_NRE] WOGHERRN, ISNAIL AHNAOI [AOORESS] [AOORESS2] [CITY_STATE] [PAONINCE] [COUNTRY] [GATE_OF_BIRTH] [101] TITESTezeaen [ LINE ]2 F2112a7a7112912345 - Natch 1 [Field Based Scanning Risk Entity Natch] * Name : mogreaen, ismail AHNeOI Address City/state country Origin Accuity Risk Entity Comments : AOOITIONAL SANCTIONS INFOANATION-SUBJECT TO SECONDARY SANCTIONS Title : HEPO OF IRANIAN POLICE ESNAIL; AHNOI-MOQCOOR, ESNA'IL; MOGHROOEN, ESENEEL AHNAOI; MOGHOOOEM, ISNAIL AHNAOI Risk Entity Info : 467916 Additional information 1: : 13 Additional information 2 : SYRIA \& IRAN-HR \& IRAN-TRA Additional information 3:66/69/2011a/a/az/2913 Additional information 4 Additional information 5 Additional information 6: CHIEF, IRAN'S LeW ENFORCENENT FOACES; POLICE CHIEF; CONVANDER OF IRAN'S LAW ENFOACENENT FOACE; BRIGPOIER GENERAL Additional information 7 Additional information 8 Additional information 9 Additional information 10 Froe form Natch Text : Mogyegen, IsNaIL AHvel

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database Design For Mere Mortals

Authors: Michael J Hernandez

4th Edition

978-0136788041

More Books

Students also viewed these Databases questions

Question

13.1 Explain the strategic role of employee benefits.

Answered: 1 week ago