英文:
Populate DefaultTableModel from test file
问题
以下是翻译好的内容:
我有这个文本文件:
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);
    }
}
英文:
I have this text file:
A
B
3.00
A
B
3.00
and my view is:

I want to match each row with each column(first_row-first_column,second_row-second_column, etc..) Where have I made a mistake?
My code is as follows:
    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);
        }
    }
}
答案1
得分: 2
你可以这样做,因为在你的输入文件中有一个空行。
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);
}
英文:
you can do this as there is an blank line in your input file .
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);
    }
答案2
得分: 0
你的计数器在循环开始时递增,因此在 if 语句中的值永远不会是 0,而是 1。然后,空行被解析为 1 并进入 A 列。你可以通过多种方式解决,比如跳过空行或在行不为空时递增计数器。
英文:
Your counter is incrementing at the begining of loop, so you never have 0 but 1 on if statement. Then empty line is parsed as 1 and goes to A column. You can solve it in multpile ways like skip empty lines or increment counter if line is not empty.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。


评论