How create a DefaultTableModel with data from text file

92 Views Asked by At

I have this test file text_file My table view is enter image description here I want suit each row with each column(first_row-first_column,second_row-second_column, etc..) Where is error?

        BufferedReader infile = new BufferedReader(reader);
        String line = "";
        int counter = 0;
        String title = "";
        String author = "";
        String price = "";
        try {
            while ((line  = infile.readLine()) != null) {
                ++counter;

                if (counter == 1) {
                    title = line;
                } else if (counter == 2) {
                    author = line;
                } else if (counter == 3) {
                    price = line;
                    SimpleBook sb = new SimpleBook(title, author, price);
                    bookList.add(sb);
                    counter = 0;
                }
            }
        } catch (IOException ex) {
            Logger.getLogger(SimpleBookList.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

}
2

There are 2 best solutions below

10
0xh3xa On

The problem in reading the file, because you read line by line not read all lines

        int counter = 0;
        String title = "", author = "";
        double price = 0.0;
        while ((line = infile.readLine()) != null) {
            ++counter;
            if (counter == 1)
                title = line;
            else if (counter == 2)
                author = line;
            else if (counter == 3) {
                price = Double.parseDouble(line);
                SimpleBook sb = new SimpleBook(title, author, price);
                bookList.add(sb);
                counter = 0;
            }
        }

, or you can check readAllLines from Oracle doc

1
Nikos Koukis On
    public class SimpleBookList {

    private ArrayList<SimpleBook> bookList;

    public SimpleBookList() {
        bookList = new ArrayList<SimpleBook>();
    }

    public void add(SimpleBook sb) {
        bookList.add(sb);
    }

    public ArrayList<SimpleBook> getBooks() {
        return bookList;
    }

    public void readFromTxt(String filename) {
        File file = new File(filename);
        FileReader reader = null;
        try {
            reader = new FileReader(file);
        } catch (FileNotFoundException e) {
            System.exit(1);
        }
        BufferedReader infile = new BufferedReader(reader);
        String line = "";
        int counter = 0;
        String title = "";
        String author = "";
        String price = "";
        try {
            while ((line  = infile.readLine()) != null) {
                ++counter;
                if (counter == 1) {
                    title = line;
                } else if (counter == 2) {
                    author = line;
                } else if (counter == 3) {
                    price = line;
                    SimpleBook sb = new SimpleBook(title, author, price);
                    bookList.add(sb);
                    counter = 0;
                }
            }
        } catch (IOException ex) {
            Logger.getLogger(SimpleBookList.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

}