将csv文件导入java swing表

时间:2011-04-04 19:48:04

标签: java swing netbeans

我在nyse中有一个所有股票报价的csv文件。第一列是符号第二列是公司的名称。

我有一个使用java swing库在netbeans中创建的搜索框和表格。

现在,当我在框中输入名称时,它返回正确的行数。因此,例如,如果我搜索GOOG,它将只返回2行(GOOG符号为1行,完整公司名称为1行)。但是,行中的数据不正确,只是反复打印csv文件的第一行。这是单击搜索按钮时执行的代码:

package my.Stock;


import java.util.ArrayList;
import java.util.Scanner;
import java.io.BufferedReader;
import java.util.StringTokenizer;
import java.io.FileReader;
import java.io.*;

public class searchy {

    public static void doSearch(String s){


                                  javax.swing.JTable resTable = StockUI.stockUI.getResultTable();
                                  javax.swing.table.DefaultTableModel dtm =
                                          (javax.swing.table.DefaultTableModel) resTable.getModel();

                                  while (dtm.getRowCount()> 0 ) dtm.removeRow(0);





            String sym = s.trim().toUpperCase();

try {

//csv file containing data
String strFile = "companylist.csv";

//create BufferedReader to read csv file
BufferedReader br = new BufferedReader( new FileReader(strFile));
String strLine = "";
StringTokenizer st = null;
int lineNumber = 0, tokenNumber = 0;

//create arraylist
ArrayList<String> arrayList = new ArrayList<String>();

//read comma separated file line by line
while( (strLine = br.readLine()) != null){

lineNumber++;

//break comma separated line using ","
st = new StringTokenizer(strLine, ",");

while(st.hasMoreTokens()){

//display csv values
tokenNumber++;

arrayList.add(st.nextToken());
//System.out.println("Line # " + lineNumber + ": "+ st.nextToken() + " " + st.nextToken());
} //end small while

//reset token number
tokenNumber = 0;

} //end big while loop

//send csv to an array
Object[] elements = arrayList.toArray();
/*
for(int i=0; i < elements.length ; i++)    {
       System.out.println(elements[i]);

} */
Scanner input = new Scanner(System.in);
System.out.print("Enter Ticker symbol");
//String sym = input.next().toUpperCase(); //convert to uppercase to match csv
int j=0;
for(int i=0; i < elements.length ; i++)    {
  if (((String) elements[i]).contains(sym)){
      //System.out.println(elements[i]);

      dtm.addRow(elements);
      j++;
      if (j==25) break; //only show this many results
  }


}

}
catch(Exception e){
System.out.println("Exception while reading csv file: " + e);
}
    }
}

我理解为什么会这样,但我不知道怎么告诉它打印正确的行,因为我不能使用dtm.addRow(elements [i]);

非常感谢任何帮助。

3 个答案:

答案 0 :(得分:1)

尝试CSVManager

答案 1 :(得分:0)

我收集来自雅虎的股票的csv数据,奇怪的是,他们不时地使用带有逗号的公司名称(例如“Dolby,Inc。”)来搞乱它。当然,这会导致解析CSV文件。我不知道这可能是你的问题。

John Doner

答案 2 :(得分:0)

    package recommendation.event.test;

    import java.io.FileReader;
    import com.csvreader.CsvReader;
    public class ReadCSV {
    public static void main (String [] args){
    try {


        CsvReader products = new CsvReader("resources/Event Recommendation Engine Challenge/data/test.csv");

        products.readHeaders();

        while (products.readRecord())
        {
            String user = products.get("user");
            String event = products.get("event");
            String invited = products.get("invited");
            String timestamp = products.get("timestamp");

            System.out.println(user + " : " + event+" : "+invited+" : "+timestamp);
        }

        products.close();
    }catch (Exception e) {
        // TODO: handle exception
    }

        }
    }