简体   繁体   中英

Import CSV file into JTable without CSVReader

How would I import a CSV file into a JTable without using CSVReader . I am currently using a scanner but I am stuck on getting the data from the CSV file into an Object[][] .

Your could do something like this.

ArrayList<String[]> data = new ArrayList<String[]>();
while( myScanner.hasNextLine()) {
    //get one row   
    String oneRow = myScanner.nextLine();
    //then split the line by comma
    String[] oneSplitRow = oneRow.split(",");
    //add your data to the arraylist
    data.add(oneSplitRow);
}
// now to make the Object[][]
Object[][] theData = new Object[data.size()][data.get(0).length];
// and here is where you can iterate through the data arraylist and put
//  the strings into theData 2d array
// ..... your code here

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM