Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Need help to rewrite below Java code. Currently it is not distinguishing src1 and src2. I need to write into new text file only src2

Need help to rewrite below Java code. Currently it is not distinguishing src1 and src2. I need to write into new text file only src2 related transactions, but it is writing src1 as well.

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 = "sampleFile.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; } else { continue; } }

// 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 is_eligible = true; 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; } else { continue; } }

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 sample input file with only 2 records attached

image text in transcribed

Expected output file with 1 output record attached below. Please test your code before sending it to me.

image text in transcribed

Fe112a7a7112912345 - Natch 1 [Field Based Scanning Risk Entity Natch] Fe1129797112912345 aga Y2B AUTO HOL 67 DEC 24 e9:11:32 89.e 85.6 191.e SYSTEM Y28 Fe1129797112912345 - Transaction Details: Free farm: [HULL_NeNE] MOGHEORN, ISNAIL AHNAOI [AOORESS] [AOORESS2] [CITY STATE] [PAONINCE] [COUNTKY] [OATE_OF_BIRTH] [101] TITESTezeaen [ LINE ]2 Fe112a7a7112912345 - Natch 1 [Field Based Scanning Risk Entity Natch] Date: 23 SEP 20 Time: 8:40 Transaction Report TRANSACTION NUMBER AMOUNT CNCY SVC DISPOSITION DATE/TIME F01120707112912345 .000 CCX AUTO HOLD 07 DEC 2409:11:3289.085.0101.0 SYSTEM F01120707112912345 - Transaction Details: Format type :U Request type :S CriteriaSetTransactionLocation::CCX Rule Based Scanning : Applied Free Form: [ID] TESTO1ADAM 12345678 [FULL_NAME]ESAH, WALI ADAM [ADDRESS] [ADDRESS2] [CITY_STATE] [PROVINCE] [COUNTRY] [DATE_OF_BIRTH] [ID1] ||TESTO1ADAM [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 : : MAULANA Title : MAULANA Remarks : ALVI, MOHAMMAD MASOOD AZHAR Risk Entity Info : 407910 Additional information 1:07/10/196803 Additional information 2: SDGT Additional information 3:11/04/2010 05/30/2019 Additional information 4: PAdional information 5: PAKISTAN PUNJAB PROVINCE Additional information 6:407904 Additional information 9: PAKISTAN Additional information 10: : STRONG Free Form Match Text : ESAH, WALI ADAM Total Transaction Count :2 Total sre Transaction Count : 1

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_2

Step: 3

blur-text-image_3

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

Graph Database Modeling With Neo4j

Authors: Ajit Singh

2nd Edition

B0BDWT2XLR, 979-8351798783

More Books

Students also viewed these Databases questions

Question

What is the client/server model?

Answered: 1 week ago