How can I populate my JTable with Date and Time from a csv file?

70 views Asked by At

So here is the link http://www2.hawaii.edu/~takebaya/ics111/jtable_basic/jtable_basic.html that I had been attempting to convert.

2011-12-22,12:28:51,12:28:53
2012-10-22,12:28:57,12:28:59
2010-10-22,12:29:10,12:29:12

Basically what I want is for my JTable to display the content of my csv file here:

package SimpleBook;

import java.time.DateTimeException;
import java.time.LocalDate;
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.io.*;

public class SimpleBookList extends DateTimeException {
    private ArrayList<SimpleBook> bookList;
    public SimpleBookList() {
        super("");
        bookList = new ArrayList<SimpleBook>();
    }
    public void add(SimpleBook sb) {
        bookList.add(sb);
    }
    public void readFromCSV(String filename) {
        File file = new File(filename);
        FileReader reader = null;

        try {
            reader = new FileReader(file);
        }
        catch (FileNotFoundException e) {
            e.printStackTrace();
            System.exit(1);
        }
        BufferedReader infile = new BufferedReader(reader);
        String line = "";
        DateTimeFormatter dateFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
        DateTimeFormatter timeFormatter = DateTimeFormatter.ofPattern("HH:mm:ss");
        try {
            boolean done = false;
            while (!done) {
                line = infile.readLine();
                if (line == null) {
                    done = true;
                }
                else {
                    String[] tokens = line.split(",");
                    LocalDate date = LocalDate.parse(tokens[0], dateFormatter);
                    LocalTime clockIn = LocalTime.parse(tokens[0], timeFormatter);
                    LocalTime clockOut = LocalTime.parse(tokens[0], timeFormatter);
                    SimpleBook sb = new SimpleBook(date,clockIn,clockOut);
                    bookList.add(sb);
                }
            }
        }
        catch (Exception e) {
            e.printStackTrace();
            System.exit(1);
        }
    }
    public Object[][] convert2Data() {
        Object[][] data = new Object[bookList.size()][3];
        for (int i = 0; i < bookList.size(); i++) {
            data[i][0] = bookList.get(i).getDate();
            data[i][1] = bookList.get(i).getClockIn();
            data[i][2] = bookList.get(i).getClockOut();
        }
       return data;
    }
}

Error Message:

java.time.format.DateTimeParseException: Text '2011-12-22' could not be parsed at index 2 at java.base/java.time.format.DateTimeFormatter.parseResolved0(DateTimeFormatter.java:2052)
    at java.base/java.time.format.DateTimeFormatter.parse(DateTimeFormatter.java:1954)
    at java.base/java.time.LocalTime.parse(LocalTime.java:465)
    at SimpleBook.SimpleBookList.readFromCSV(SimpleBookList.java:44)
    at SimpleBook.BasicJTable.<init>(BasicJTable.java:25)
    at SimpleBook.BasicJTable.main(BasicJTable.java:40)
0

There are 0 answers