Java 从测试文件填充DefaultTableModel

Java 从测试文件填充DefaultTableModel,java,Java,我有这个文本文件: A B 3.00 A B 3.00 我的看法是: 我想将每一行与每一列匹配(第一行-第一列,第二行-第二列,等等),我在哪里犯了错误? 我的代码如下: BufferedReader infile = new BufferedReader(reader); String line = ""; int counter = 0; String title = ""; String author = "";

我有这个文本文件:

A
B
3.00

A
B
3.00
我的看法是:

我想将每一行与每一列匹配(第一行-第一列,第二行-第二列,等等),我在哪里犯了错误? 我的代码如下:

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

}

您的计数器在循环的开始处递增,所以if语句上的值不是0而是1。然后将空行解析为1并转到一列。您可以通过多文件方式来解决它,例如跳过空行或在行不为空时递增计数器。

您的计数器在循环开始时递增,所以if语句中的值永远不会是0,而是1。然后将空行解析为1并转到一列。您可以通过多文件方式解决它,如跳过空行或在行不为空时增加计数器。

您可以这样做,因为输入文件中有一个空行

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

            ++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);
    }

您可以这样做,因为输入文件中有一个空行

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

            ++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);
    }