填充DefaultTableModel从测试文件

huangapple go评论60阅读模式
英文:

Populate DefaultTableModel from test file

问题

以下是翻译好的内容:

我有这个文本文件:

A
B
3.00

A
B
3.00

我的视图是:

填充DefaultTableModel从测试文件

我想要将每一行与每一列进行匹配(第一行-第一列,第二行-第二列,依此类推)。我在哪里犯了一个错误?
我的代码如下:

    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:

填充DefaultTableModel从测试文件

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.

huangapple
  • 本文由 发表于 2020年5月30日 20:38:15
  • 转载请务必保留本文链接:https://go.coder-hub.com/62102548.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定