Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8375155
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T15:06:03+00:00 2026-06-09T15:06:03+00:00

Ive been working on this code for quite sometime and just want to be

  • 0

Ive been working on this code for quite sometime and just want to be given the simple heads up if im routing down a dead end. The point where im at now is to mathch identical cells from diffrent .csv files and copy one row into another csv file. The question really is would it be possible to write at specfic lines say for example if the the 2 cells match at row 50 i wish to write back on to row 50. Im assuming that i would maybe extract everything to a hashmap, write it in there then write back to the .csv file? is there a easier way?

for example i have one Csv that has person details, and the other has property details of where the actual person lives, i wish to copy the property details to the person csv, aswell as match them up with the correct person detail. hope this makes sense

public class Old {
 public static void main(String [] args) throws IOException
 {
   List<String[]> cols;
   List<String[]> cols1;

   int row =0;
   int count= 0;
   boolean b;
   CsvMapReader Reader = new CsvMapReader(new FileReader("file1.csv"), CsvPreference.EXCEL_PREFERENCE);
   CsvMapReader Reader2 = new CsvMapReader(new FileReader("file2.csv"), CsvPreference.EXCEL_PREFERENCE);

   try {
       cols = readFile("file1.csv");
       cols1 = readFile("fiel2.csv");
       String [] headers = Reader.getCSVHeader(true);

       headers = header(cols1,headers          

           } catch (IOException e) {
       e.printStackTrace();
       return;
   }

   for (int j =1; j<cols.size();j++) //1
   {
       for (int i=1;i<cols1.size();i++){
           if (cols.get(j)[0].equals(cols1.get(i)[0]))
           {


           }
       }

   }

}


private static List<String[]> readFile(String fileName) throws IOException
{
   List<String[]> values = new ArrayList<String[]>();
   Scanner s = new Scanner(new File(fileName));
   while (s.hasNextLine()) {
       String line = s.nextLine();
       values.add(line.split(","));
   }
   return values;
}
public static void csvWriter (String fileName, String [] nameMapping ) throws FileNotFoundException
{
    ICsvListWriter writer = new CsvListWriter(new PrintWriter(fileName),CsvPreference.STANDARD_PREFERENCE);
    try {
        writer.writeHeader(nameMapping);

    } catch (IOException e) {

        e.printStackTrace();
    }

}
public static String[] header(List<String[]> cols1, String[] headers){
    List<String> list = new ArrayList<String>();
    String [] add;
    int count= 0;
    for (int i=0;i<headers.length;i++){
        list.add(headers[i]);
    }

    boolean c;
    c= true;
    while(c)        {           
        add = cols1.get(0);
        list.add(add[count]);
        if (cols1.get(0)[count].equals(null))// this line is never read errpr
        {               
            c=false;
            break;
        } else  
        count ++;

    }

    String[] array = new String[list.size()];
    list.toArray(array);
    return array;

}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T15:06:05+00:00Added an answer on June 9, 2026 at 3:06 pm

    Just be careful if you read all of the addresses and person details into memory first (as Thomas has suggested) – if you’re only dealing with small CSV files then it’s fine, but you may run out of memory if you’re dealing with larger files.

    As an alternative, I’ve put together an example that reads the addresses in first, then writes the combined person/address details while it reads in the person details.

    Just a few things to note:

    • I’ve used CsvMapReader and CsvMapWriter because you were – this meant I’ve had to use a Map containing a Map for storing the addresses. Using CsvBeanReader/CsvBeanWriter would make this a bit more elegant.

    • The code from your question doesn’t actually use Super CSV to read the CSV (you’re using Scanner and String.split()). You’ll run into issues if your CSV contains commas in the data (which is quite possible with addresses), so it’s a lot safer to use Super CSV, which will handle escaped commas for you.

    Example:

    package example;
    
    import java.io.StringReader;
    import java.io.StringWriter;
    import java.util.HashMap;
    import java.util.Map;
    
    import org.supercsv.io.CsvMapReader;
    import org.supercsv.io.CsvMapWriter;
    import org.supercsv.io.ICsvMapReader;
    import org.supercsv.io.ICsvMapWriter;
    import org.supercsv.prefs.CsvPreference;
    
    public class CombiningPersonAndAddress {
    
        private static final String PERSON_CSV = "id,firstName,lastName\n"
                + "1,philip,fry\n2,amy,wong\n3,hubert,farnsworth";
    
        private static final String ADDRESS_CSV = "personId,address,country\n"
                + "1,address 1,USA\n2,address 2,UK\n3,address 3,AUS";
    
        private static final String[] COMBINED_HEADER = new String[] { "id",
                "firstName", "lastName", "address", "country" };
    
        public static void main(String[] args) throws Exception {
    
            ICsvMapReader personReader = null;
            ICsvMapReader addressReader = null;
            ICsvMapWriter combinedWriter = null;
            final StringWriter output = new StringWriter();
    
            try {
                // set up the readers/writer
                personReader = new CsvMapReader(new StringReader(PERSON_CSV),
                        CsvPreference.STANDARD_PREFERENCE);
                addressReader = new CsvMapReader(new StringReader(ADDRESS_CSV),
                        CsvPreference.STANDARD_PREFERENCE);
                combinedWriter = new CsvMapWriter(output,
                        CsvPreference.STANDARD_PREFERENCE);
    
                // map of personId -> address (inner map is address details)
                final Map<String, Map<String, String>> addresses = 
                        new HashMap<String, Map<String, String>>();
    
                // read in all of the addresses
                Map<String, String> address;
                final String[] addressHeader = addressReader.getCSVHeader(true);
                while ((address = addressReader.read(addressHeader)) != null) {
                    final String personId = address.get("personId");
                    addresses.put(personId, address);
                }
    
                // write the header
                combinedWriter.writeHeader(COMBINED_HEADER);
    
                // read each person
                Map<String, String> person;
                final String[] personHeader = personReader.getCSVHeader(true);
                while ((person = personReader.read(personHeader)) != null) {
    
                    // copy address details to person if they exist
                    final String personId = person.get("id");
                    final Map<String, String> personAddress = addresses.get(personId);
                    if (personAddress != null) {
                        person.putAll(personAddress);
                    }
    
                    // write the combined details
                    combinedWriter.write(person, COMBINED_HEADER);
                }
    
            } finally {
                personReader.close();
                addressReader.close();
                combinedWriter.close();
            }
    
            // print the output
            System.out.println(output);
    
        }
    }
    

    Output:

    id,firstName,lastName,address,country
    1,philip,fry,address 1,USA
    2,amy,wong,address 2,UK
    3,hubert,farnsworth,address 3,AUS
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've been working on this code to implement a clickable Share button for ShareThis,
Why isn't this code working? I've been stuck on this for 2 days. public
In a project I'm working on (I picked up this code and I've been
I've recently been working with code that looks like this: using namespace std; class
I've been working on this elevator program for quite some time now and finally
I've been working on this for quite some time looking for solutions in various
I have been working on this problem for quite some time and can't figure
I've been working on this site for quite a while, and I've finally got
I want to unit test the code below. I've been working with MSTest and
I've been working on this assignment, where I need to read in records and

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.