Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

I need help with below Java code to distinguish between src1 and src2. Read thru file and only write into the file source from src2.

I need help with below Java code to distinguish between src1 and src2. Read thru file and only write into the file source from 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 = "C:\\Users\\mua2643\\OneDrive - MUFG Union Bank\\MUB_Dedicated\\FileProcessingApproach3\\src\\FileProcessing\\one_MB_test_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 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 header file where the src1 and src2 need to be distinguished.

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

F01120707112912346 .000 Y28 AUTO HOLD 07 DEC 24 09:11:32 89.0 85.0 101.0 SYSTEM Y28

F01120707112912347 .000 D69 AUTO HOLD 07 DEC 24 09:11:32 89.0 85.0 101.0 SYSTEM D69

F01120707112912348 .000 NYB AUTO HOLD 07 DEC 24 09:11:32 89.0 85.0 101.0 SYSTEM NYB

F01120707112912349 .000 AES AUTO HOLD 07 DEC 24 09:11:32 89.0 85.0 101.0 SYSTEM AES

F01120707112912350 .000 CIF AUTO HOLD 07 DEC 24 09:11:32 89.0 85.0 101.0 SYSTEM CIF

F01120707112912351 .000 WKD AUTO HOLD 07 DEC 24 09:11:32 89.0 85.0 101.0 SYSTEM WKD

F01120707112912352 .000 MSB AUTO HOLD 07 DEC 24 09:11:32 89.0 85.0 101.0 SYSTEM MSB

F01120707112912353 .000 SEI AUTO HOLD 07 DEC 24 09:11:32 89.0 85.0 101.0 SYSTEM SEI

F01120707112912354 .000 PTT AUTO HOLD 07 DEC 24 09:11:32 89.0 85.0 101.0 SYSTEM PTT

F01120707112912355 .000 TD1 AUTO HOLD 07 DEC 24 09:11:32 89.0 85.0 101.0 SYSTEM TD1

F01120707112912356 .000 TD2 AUTO HOLD 07 DEC 24 09:11:32 89.0 85.0 101.0 SYSTEM TD2

F01120707112912357 .000 TD3 AUTO HOLD 07 DEC 24 09:11:32 89.0 85.0 101.0 SYSTEM TD3

F01120707112912358 .000 TSP AUTO HOLD 07 DEC 24 09:11:32 89.0 85.0 101.0 SYSTEM TSP

F01120707112912359 .000 TSX AUTO HOLD 07 DEC 24 09:11:32 89.0 85.0 101.0 SYSTEM TSX

F01120707112912360 .000 PAP AUTO HOLD 07 DEC 24 09:11:32 89.0 85.0 101.0 SYSTEM PAP

F01120707112912360 .000 USR AUTO HOLD 07 DEC 24 09:11:32 89.0 85.0 101.0 SYSTEM USR

Attached sample file screenshot as well.

image text in transcribed

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 Concepts

Authors: David M. Kroenke, David J. Auer

7th edition

133544621, 133544626, 0-13-354462-1, 978-0133544626

More Books

Students also viewed these Databases questions

Question

Is the level of information in line with the original goals?

Answered: 1 week ago

Question

How much access do people have to internet and e-mail?

Answered: 1 week ago

Question

How many people will participate in the programme?

Answered: 1 week ago