0

私は両方とも主キーを持つ2つのcsvを持っています。関連するcsvを正しいキーを持つものに組み合わせることができるように、両方のキーを共マップしようとしていますが、今のところ、列の1つにあるものを比較したかっただけです。私の心は少し空白です。コードによって、私が進む方向を理解できることを願っています。配列に実際に文字列を取り込むことができません。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++;

            }

        }


    }
    }

}

}

4

1 に答える 1

3

colsとに配列を使用する代わりに、 を使用cols1する必要がありますList<String[]>。また、比較ループが読み取りループと絡み合っているため、コードは非常に混乱しています。最初にデータを読み込んでから比較を行うほうがよいでしょう。

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;
}
于 2012-07-25T16:20:16.010 に答える