2016-03-08 51 views
-2

我試圖讀取一個csv文件,我創建了一個測試文件,其中有9個條目和它們的值,但是我的代碼不會讀取第二行,它說該文件沒有被發現通過第二個鍵,我試過儘可能多的tweeking,有人可以幫我嗎?示例輸入將包括類似這樣的東西,但在一個csv文件(所以每個在一個新的行中,我在這裏是新的,仍然在學習編輯文本在這裏): Diego,2 Maria,2 Armando,5 Ken,1讀取CSV文件,arrayindex越界錯誤

public static void main(String[] args) { 

    HashMap<String, Integer> h = new HashMap<String, Integer>(511); 


    try 
    { 
     Scanner readIn = new Scanner (new File ("test1.csv")); 
     System.out.println("I'm here 1"); 


     while (readIn.hasNext()) 
     {    

      System.out.print(readIn.next());// for testing purposes only 
      System.out.println("Check 2"); // for testing purposes only 


      String line = readIn.nextLine(); 
      String str[] = line.split(","); 

      for (int i = 0; i < str.length ; i++) 
      { 
       String k = str[0]; 
       int v = Integer.parseInt(str[1]); 
       h.insert(k , v); 
      } 

      System.out.println(h.toString()); 

     } 
     readIn.close();  
    } 

    catch (ArrayIndexOutOfBoundsException ob) 
    { 
     System.out.println(" - The file wasn't found."); 
    } 

    catch (FileNotFoundException e) 
    { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } 
} 
+0

請提供一些示例輸入。 – klog

+0

我試過了,這些名稱,數字對中的每一個都在一個新行中 – Maria

+0

您可以請您提供您所看到的錯誤輸出嗎?我想這行System.out.print(readIn.next());可能會丟掉你的代碼。 – klog

回答

0

一個調用next()或nextLine()應與調用hasNext()來preceeded,但在你的代碼你檢查是否hasNext()在while循環返回true,然後調用這兩個循環內的next()和nextLine()。 您可以如下修改代碼:

while (readIn.hasNext()) 
      {    

       String line = readIn.nextLine(); 
       String str[] = line.split(","); 

       for (int i = 0; i < str.length ; i++) 
       { 
        String k = str[0]; 
        int v = Integer.parseInt(str[1]); 
        h.put(k , v); 
       } 

       System.out.println(h.toString()); 

      } 
+0

謝謝!這工作。請問爲什麼需要先調用hasNext()? – Maria

+0

掃描儀文檔(http://docs.oracle.com/javase/6/docs/api/java/util/Scanner.html)提供瞭如何使用每種方法的詳細信息。這將說明問題。 –

0

您的循環是不實際的服務目的。你會注意到你從來沒有在循環中實際引用i。在OP的回答之前,我相信你試圖分割一個沒有逗號的字符串,但是你的代碼假定一個字符串會在那裏,因此出現了越界異常。這與我爲什麼告訴你println()有問題有關。

至於你對hasNext()的問題,這是你知道你可以從文件中讀取另一行的唯一方法。如果你嘗試閱讀過去的話,你會遇到問題。

0

寧可寫代碼自己讀取CSV文件。我建議你使用像Apache Commons CSV這樣的標準庫。它提供了更多的方法來處理CSVTab separated file,等...

import java.io.FileReader; 
import java.util.List; 

import org.apache.commons.csv.CSVFormat; 
import org.apache.commons.csv.CSVRecord; 

public class SO35859431 { 
    public static void main(String[] args) { 
     String filePath = "D:\\user.csv"; 
     try { 
      List<CSVRecord> listRecords = CSVFormat.DEFAULT.parse(new FileReader(filePath)).getRecords(); 
      for (CSVRecord csvRecord : listRecords) { 
       /* Get record using index/position */ 
       System.out.println(csvRecord.get(0)); 
      } 
     } catch (Exception e) { 
      e.printStackTrace(); 
     } 
    } 
} 
0

首先出現的是在HashMap中class.The沒有這樣的insert()方法正確放置(K,V)&在while循環它應該是hasNext()。 Follow是我的代碼替代了BufferedReader。

/* *要更改此許可證標題,請在項目屬性中選擇許可證標題。 *要更改此模板文件,請選擇工具|模板 *並在編輯器中打開模板。 */ 包讀取;

import java.io.BufferedReader; 
import java.io.File; 
import java.io.FileInputStream; 
import java.io.FileNotFoundException; 
import java.io.FileReader; 
import java.io.IOException; 
import java.io.InputStreamReader; 
import java.util.HashMap; 
import java.util.Scanner; 

/** 
* 
* @author Isuru Rangana Jr 
*/ 
public class Read { 

    /** 
    * @param args the command line arguments 
    */ 
    public static void main(String args[]) throws FileNotFoundException, IOException { 
     HashMap<String, Integer> h = new HashMap<String, Integer>(); 

     try { 
      BufferedReader readIn = new BufferedReader(new FileReader(new File("test.csv"))); 

      while (readIn.ready()) { 

       String line = readIn.readLine(); 
       String str[] = line.split(","); 


       for (int i = 0; i < str.length; i++) { 
        String k = str[0]; 
        int v = Integer.parseInt(str[1]); 
        h.put(k, v); 
       } 

       System.out.println(h.toString()); 

      } 
      readIn.close(); 
     } catch (ArrayIndexOutOfBoundsException ob) { 
      System.out.println(" - The file wasn't found."); 
     } 
    } 

}