简体   繁体   中英

Comparing 2 columns in 2 csv files in Java

I have two csv both with a primary key, im trying to comapre both keys so that i can combine the related csvs to one with the correct keys, however for now i just wanted to compare what was in collumns one. My mind is a little blank i hope by the code you can sort of figure out the direction im going. im unable to get my arrays to actually take in the strings . Java.lang.arrayindexoutofboundsexception

import java.io. ; import java.util. ;

public class UDC { public void search(String [][] wat, String [][] ud){ } public static void main (String [] args) throws IOException { String [] [] cols = {}; String [] [] cols1={}; int row =0; int row1 =0; int j = 0; /*Scanner s = new Scanner(new File("Watford Update File.csv")); Scanner c = new Scanner(new File("udc.csv")); while (s.hasNextLine()){ String line = s.nextLine(); cols =line.split(","); System.out.println(cols[0]);*/ Scanner s = new Scanner(new File("file1.csv")); Scanner w = new Scanner (new File("file.csv")); while (w.hasNextLine()) { String line1 = w.nextLine(); System.out.println(cols); //cols[row]=line1.split(","); row ++; if(!w.hasNextLine()){ while (s.hasNextLine()){ String line2 = s.nextLine(); //cols1[row1]=line2.split(","); //put while loop in diffrent method but break if(cols[j].equals(cols1[row1])) { j++; row1++; System.out.print(cols[j]); System.out.print(" "); System.out.print(cols1[row1]); System.out.println(); }else{ row1++; } } } } }

 public class UDC { public void search(String [][] wat, String [][] ud){ } public static void main (String [] args) throws IOException { String [] [] cols = {}; String [] [] cols1={}; int row =0; int row1 =0; int j = 0; /*Scanner s = new Scanner(new File("Watford Update File.csv")); Scanner c = new Scanner(new File("udc.csv")); while (s.hasNextLine()){ String line = s.nextLine(); cols =line.split(","); System.out.println(cols[0]);*/ Scanner s = new Scanner(new File("file1.csv")); Scanner w = new Scanner (new File("file.csv")); while (w.hasNextLine()) { String line1 = w.nextLine(); System.out.println(cols); //cols[row]=line1.split(","); row ++; if(!w.hasNextLine()){ while (s.hasNextLine()){ String line2 = s.nextLine(); //cols1[row1]=line2.split(","); //put while loop in diffrent method but break if(cols[j].equals(cols1[row1])) { j++; row1++; System.out.print(cols[j]); System.out.print(" "); System.out.print(cols1[row1]); System.out.println(); }else{ row1++; } } } } } 

}

Instead of using arrays for cols and cols1 , you should use List<String[]> . Also, your code is very confusing because you have the comparison loop(s?) intertwined with the reading loops. It would be better to simply read in the data first and then do your comparisons.

public static void main(String [] args)
{
    List<String[]> cols;
    List<String[]> cols1;
    try {
        cols = readFile("udc.csv");
        cols1 = readFile("Watford Update File.csv");
    } catch (IOException e) {
        e.printStackTrace();
        return;
    }
    // loop through array lists to do your comparisons
    // For example, to compare the first column of rows i and j:
    //    cols.get(i)[0].equals(cols1.get(j)[0])
}

private static List<String[]> readFile(String fileName) throws IOException
{
    List<String[]> values = new ArrayList<String[]>();
    Scanner s = new Scanner(new File("udc.csv"));
    while (s.hasNextLine()) {
        String line = s.nextLine();
        values.add(line.split(","));
    }
    return values;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM