简体   繁体   English

比较Java中2个csv文件中的2列

[英]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. 我有两个具有主键的csv,我试图将两个键进行比较,以便可以将相关的csvs与具有正确键的一个组合,但是现在我只想比较列中的内容。 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 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[]> . 代替对colscols1使用数组,应该使用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;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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