2016-04-18 28 views
0

OPENCSVを使用して特定の列を行から読み取ろうとすると、小さな問題に直面しています。私はこのようなCSVファイルを持っていますOpenCSV特定の列を読み取る

"ID","Name","Name2","Date","Author" 
"1","Alex","Example","18.3.2016","Alex" 

今私は2列目と3列目(名前と名前2)だけを読みたいと思っています。

私のコードは、私はすでに1及び2に手動で「i」の変数を設定しようとしたが、その後、私は、ログに示されている4つの同じ結果を取得しています。この

try { 
      CSVReader reader = new CSVReader(new FileReader(filelocation)); 

      String [] nextLine; 
      int rowNumber = 0; 

      while ((nextLine = reader.readNext()) != null) { 
       rowNumber++; 
       for(int i = 0; i< nextLine.length ; i++){ 
        System.out.println("Cell index: " + i); 
        System.out.println("Cell Value: " + nextLine[i]); 
        System.out.println("---"); 
       } 
      } 
     } catch (FileNotFoundException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } 
     catch (IOException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } 

のように見えます。何が欠けている?ありがとう!

答えて

1

気にしないで、私はそのトリックを見つけました。

これはどのように見えるかです。

try { 

      CSVReader reader = new CSVReader(new FileReader(filelocation)); 

      String [] nextLine; 
      int rowNumber = 0; 
      while ((nextLine = reader.readNext()) != null) { 
       rowNumber++; 
       String name = nextLine[1]; 
       String name2 = nextLine[2]; 
      } 
     } catch (FileNotFoundException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } 
     catch (IOException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } 

その後、私は文字列の値を取得し、それを使用することができます。

関連する問題