JAVA - 将CSV导入ArrayList [英] JAVA - import CSV to ArrayList

查看:325
本文介绍了JAVA - 将CSV导入ArrayList的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试使用 StringTokenizer CSV 文件导入到 Arraylist $ c>:

  public class Test 
{
public static void main(String [] args)
{
List< ImportedXls> datalist = new ArrayList< ImportedXls>();

try
{
FileReader fr = new FileReader(c:\\temp.csv);
BufferedReader br = new BufferedReader(fr);
String stringRead = br.readLine();

while(stringRead!= null)
{
StringTokenizer st = new StringTokenizer(stringRead,,);
String docNumber = st.nextToken();
String note = st.nextToken(); / ** PROBLEM * /
String index = st.nextToken(); / ** PROBLEM * /

ImportedXls temp = new ImportedXls(docNumber,note,index);
datalist.add(temp);

//读取下一行
stringRead = br.readLine();
}
br.close();
}
catch(IOException ioe){...}

for(ImportedXls item:datalist){
System.out.println(item.getDocNumber() );
}
}
}

因为如果我保持初始化三个变量( docNumber note code>和 )作为 nextToken(),它失败:



线程main中的异常java.util.NoSuchElementException
在java.util.StringTokenizer.nextToken(未知源)
at _test .Test.main(Test.java:32)

如果我只保留docNumber,你能帮我吗?

解决方案

看起来你的输入文件的一些行少于3个逗号分隔的字段。应该总是检查tokenizer是否有更多的令牌(StringTokenizer.hasMoreTokens),除非你是100%肯定你的输入是正确的。



CORRECT解析CSV文件不是那么简单任务。为什么不使用可以做得很好的图书馆 - http://opencsv.sourceforge.net/


I'm trying import CSV file to Arraylist using StringTokenizer:

public class Test
{
  public static void main(String [] args)
  {
    List<ImportedXls> datalist = new ArrayList<ImportedXls>();

    try
    {
      FileReader fr = new FileReader("c:\\temp.csv");
      BufferedReader br = new BufferedReader(fr);
      String stringRead = br.readLine();

      while( stringRead != null )
      {
        StringTokenizer st = new StringTokenizer(stringRead, ",");
        String docNumber = st.nextToken( );
        String note = st.nextToken( );  /** PROBLEM */
        String index = st.nextToken( ); /** PROBLEM */

        ImportedXls temp = new ImportedXls(docNumber, note, index);
        datalist.add(temp);

        // read the next line
        stringRead = br.readLine();
      }
      br.close( );
    }
    catch(IOException ioe){...}

    for (ImportedXls item : datalist) {
      System.out.println(item.getDocNumber());
    }
  }
}

I don't understand how the nextToken works, because if I keep the initialize three variables (docNumber, note and index) as nextToken(), it fails on:

Exception in thread "main" java.util.NoSuchElementException
    at java.util.StringTokenizer.nextToken(Unknown Source)
    at _test.Test.main(Test.java:32)

If I keep docNumber only, it works. Could you help me?

解决方案

It seems that some of the rows of your input file have less then 3 comma separated fields.You should always check if tokenizer has more tokens (StringTokenizer.hasMoreTokens), unless you are are 100% sure your input is correct.

CORRECT parsing of CSV files is not so trivial task. Why not to use a library that can do it very well - http://opencsv.sourceforge.net/ ?

这篇关于JAVA - 将CSV导入ArrayList的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆