I have two text files which are similar but with different tags:
File 1:

Ben|2
Jen|4
Harry|8
Bill|3
 
Jack|2
Jill|2
Rip|1
 
P|1
A|3
B|4
C|2
 
G|5
 
Tom|1
Harr|4
Red|4
Green|2
 
Red|3
Blue|4
Lent|4
Burrow|2

File 2:

Ben|2
Jen|5
Harry|2
Bill|4
 
Jack|3
Jill|6
Rip|1
 
P|2
A|19
B|0
C|1
 
G|2
 
Tom|3
Harr|2
Red|3
Green|4
 
Red|3
Blue|4
Lent|4
Burrow|2

Objective is to read both the files and whenever I see a word with tag "something" in File 1 and need to printout the word with the tag given in file 2.
But whenever I see a word with tag "4" in file 1 like this:

from file 1:
 
Tom|1
Harr|4
Red|4
Green|2

Output should be:

etc
 
Tom|3
Harr|2
Red|3
Green|4

 
Tom|3
Harr|2
Red|3
Green|4
 
etc

Basically if I see a tag 4in file 1 I need to generate multiple copies of my block in file2.
How can I do it?

I am trying like this:

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.InputStreamReader;
import java.io.PrintStream;
 
 
public class trysplit6New{
	public static void main(String args[]) throws Exception{
		//trysplit4 is final
		
		int flag = 0;
		//String filename="data/split3.txt";
		System.setOut(new PrintStream(new FileOutputStream("Data/outputnew2.txt")));
		
		String filename1="data/split3.txt";
		BufferedReader br1 = new BufferedReader(new InputStreamReader(new FileInputStream(filename1)));
		
		String filename2="data/split4.txt";
		BufferedReader br2 = new BufferedReader(new InputStreamReader(new FileInputStream(filename2)));
				
		String data_copy = null; //read in split3
		String[] temp1 = new String[10000]; // to store
		String data_copy2 = null; //to read split4
		String[] temp2 = new String[10000];
		
		int count=0;
		while((data_copy= br1.readLine())!=null){
			while((data_copy2 = br2.readLine())!=null){
				//if(!data_copy.equals("") && !data_copy2.equals("")){
				if(!data_copy2.equals("")){
					if(!data_copy.equals("")){
				 temp1[count]=data_copy;
				 temp2[count]=data_copy2;
				 count=count+1;
				}}
				else{
			
					//CHECK WHETHER THERE IS A 4 INSIDE	
				 flag = 0;
				 for(int i=0;i<count;i++){
					 // IF THERE A 4 
					if(temp1[i].split("\\|")[1].equals("4")){
						 flag+=1;
					}
				
				else  flag+=0;
			}
			
			if(flag>0){
			for(int i=0;i<count;i++){
			
				
				if(temp1[i].split("\\|")[1].equals("4")){
										
					for(int j=0;j<i;j++){
						
						//System.out.println(temp1[j].split("\\|")[0] + "|" + temp2[j].split("\\|")[1]);
						System.out.println(temp2[j].split("\\|")[0] + "|" + temp2[j].split("\\|")[1]);
					}
					//System.out.println(temp1[i].split("\\|")[0] + "|" + temp2[i].split("\\|")[1]);
					System.out.println(temp2[i].split("\\|")[0] + "|" + temp2[i].split("\\|")[1]);
					
					
					for(int j=i+1;j<count;j++){
						//System.out.println(temp1[j].split("\\|")[0] + "|" + temp2[j].split("\\|")[1]);
						System.out.println(temp2[j].split("\\|")[0] + "|" + temp2[j].split("\\|")[1]);
						
					}
					System.out.println();
				}
												
							
			}
			}
			else {
				for(int i=0;i<count;i++){
					//System.out.println(temp1[i].split("\\|")[0] + "|" + temp2[i].split("\\|")[1]);
					
					System.out.println(temp2[i].split("\\|")[0] + "|" + temp2[i].split("\\|")[1]);
				}
				System.out.println();
			}
   			//temp1 = new String[100];
			temp1 = new String[10000];
			temp2 = new String[10000];
			
			count=0;
			//System.out.println();
			
				
			}
			
		}
			
				
		//--------------
		//CHECK WHETHER THERE IS A 4 INSIDE	
		 flag = 0;
		for(int i=0;i<count;i++){
			// IF THERE A 4 
			if(temp1[i].split("\\|")[1].equals("4")){
				 flag+=1;
			}
			
			else  flag+=0;
		}
		
		if(flag>0){
		for(int i=0;i<count;i++){
		
			
			if(temp1[i].split("\\|")[1].equals("4")){
									
				for(int j=0;j<i;j++){
					
					//System.out.println(temp1[j].split("\\|")[0] + "|" + temp2[j].split("\\|")[1]);
					System.out.println(temp2[j].split("\\|")[0] + "|" + temp2[j].split("\\|")[1]);
				}
				//System.out.println(temp1[i].split("\\|")[0] + "|" + temp2[i].split("\\|")[1]);
				System.out.println(temp2[i].split("\\|")[0] + "|" + temp2[i].split("\\|")[1]);
				
				
				for(int j=i+1;j<count;j++){
					//System.out.println(temp1[j].split("\\|")[0] + "|" + temp2[j].split("\\|")[1]);
					System.out.println(temp2[j].split("\\|")[0] + "|" + temp2[j].split("\\|")[1]);
				}
				System.out.println();
			}
			
					
			
						
		}
		
		}
		else {
			for(int i=0;i<count;i++){
				//System.out.println(temp1[i].split("\\|")[0] + "|" + temp2[i].split("\\|")[1]);
				System.out.println(temp2[i].split("\\|")[0] + "|" + temp2[i].split("\\|")[1]);
				
			}
			System.out.println();
		}
		//temp1 = new String[100];
		temp1 = new String[10000];
		temp2 = new String[10000];
		count=0;
		//System.out.println();
		
			
		}
		
	}
}

My attempt generates the output like this:

Ben|2
Jen|5
Harry|2
Bill|4
 
Jack|3
Jill|6
Rip|1
 
P|2
A|19
B|0
C|1
 
G|2
 
Tom|3
Harr|2
Red|3
Green|4
 
Red|3
Blue|4
Lent|4
Burrow|2

Which is basically file2. This is half the objective achieved. I think I am wrong in reading temp1. It is never reading tag "4".
Can someone help me fix my code?

I have solved the problem myself. Help not needed. Thanks

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.