0

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)
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
  • In which way *doesn’t work*? Does it show nothing? Are you getting any error messages? – Ole V.V. Apr 12 '21 at 08:39
  • 1
    java.time.format.DateTimeParseException: Text '2011-12-22' could not be parsed at index 10 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.LocalDate.parse(LocalDate.java:430) at SimpleBook.SimpleBookList.readFromCSV(SimpleBookList.java:42) at SimpleBook.BasicJTable.(BasicJTable.java:25) at SimpleBook.BasicJTable.main(BasicJTable.java:40) – encore mod Apr 12 '21 at 08:42
  • This kind of error message pops up – encore mod Apr 12 '21 at 08:43
  • You have three entries per line. One is formatted yyyy-MM-dd, the other two uses format HH:mm:ss. And in your sourcecode you use as format "yyyy-MM-dd HH:mm:ss". Seems obvious why it's not working. – Ralf Renz Apr 12 '21 at 08:50
  • oh, thats why... so dateFormat should be seperated with timeFormat? – encore mod Apr 12 '21 at 08:55
  • 2
    Tip: When editing, don't use the `JavaScript/HTML/CSS snippet` button (on the right, in red(1)) for anything other than .. JavaScript, HTML & CSS snippets! It is a special feature that allows people to click a button and actually see the effect of rendering the code, mark-up and styles. Instead use the Code button to the left (with a green check mark above it(1)). 1) They are [shown in this screenshot](https://i.stack.imgur.com/BDM7f.png). – Andrew Thompson Apr 12 '21 at 09:04
  • 2
    *"This kind of error message pops up"* That stack trace should be added to the question as an [edit]. Of course, use code formatting for code and code snippets, structured documents like XML or input/output. To do that, select the text and click the `{}` button at the top of the message posting/editing form. – Andrew Thompson Apr 12 '21 at 09:06
  • sorry I kinda missed it, its my first post. thank you. – encore mod Apr 12 '21 at 09:07
  • *"I kinda missed it"* Missed what? Now is a good time for more words, not less. And another tip: Add @RalfRenz (or whoever, the `@` is important) to *notify* the person of a new comment. – Andrew Thompson Apr 12 '21 at 09:10
  • 2
    You want `tokens[1]` for `clockIn` and `tokens[2]` for `clockOut`. – Sal Apr 12 '21 at 17:50

0 Answers0