将csv文件导入java swing表 [英] importing a csv file into a java swing table

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

问题描述

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

I have a csv file of all the stock quotes on in the nyse. first column is symbol second column is the name of the company.

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

I have a search box and table made in netbeans using the java swing library.

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

Right now when I enter the name in the box it is returning the correct amount of rows. So for instance if I search GOOG it will only return 2 rows (1 row for the GOOG symbol and one row for the name in the full company name). However the data within the rows is not the correct ones it is just printing the first row of the csv file over and over. here is the code that gets executed when clicking the search button:

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]);

I understand why this is happening but I am not sure how to tell it to print the correct lines since I can't use dtm.addRow(elements[i]);

任何帮助都非常感谢。

Any help is greatly appreciated.

推荐答案

尝试 CSVManager

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

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