填充DefaultTableModel从测试文件

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

Populate DefaultTableModel from test file

问题

以下是翻译好的内容:

我有这个文本文件:

  1. A
  2. B
  3. 3.00
  4. A
  5. B
  6. 3.00

我的视图是:

填充DefaultTableModel从测试文件

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

  1. BufferedReader infile = new BufferedReader(reader);
  2. String line = "";
  3. int counter = 0;
  4. String title = "";
  5. String author = "";
  6. String price = "";
  7. try {
  8. while ((line = infile.readLine()) != null) {
  9. ++counter;
  10. if (counter == 1) {
  11. title = line;
  12. } else if (counter == 2) {
  13. author = line;
  14. } else if (counter == 3) {
  15. price = line;
  16. SimpleBook sb = new SimpleBook(title, author, price);
  17. bookList.add(sb);
  18. counter = 0;
  19. }
  20. }
  21. } catch (IOException ex) {
  22. Logger.getLogger(SimpleBookList.class.getName()).log(Level.SEVERE, null, ex);
  23. }
  24. }
英文:

I have this text file:

  1. A
  2. B
  3. 3.00
  4. A
  5. B
  6. 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:

  1. BufferedReader infile = new BufferedReader(reader);
  2. String line = "";
  3. int counter = 0;
  4. String title = "";
  5. String author = "";
  6. String price = "";
  7. try {
  8. while ((line = infile.readLine()) != null) {
  9. ++counter;
  10. if (counter == 1) {
  11. title = line;
  12. } else if (counter == 2) {
  13. author = line;
  14. } else if (counter == 3) {
  15. price = line;
  16. SimpleBook sb = new SimpleBook(title, author, price);
  17. bookList.add(sb);
  18. counter = 0;
  19. }
  20. }
  21. } catch (IOException ex) {
  22. Logger.getLogger(SimpleBookList.class.getName()).log(Level.SEVERE, null, ex);
  23. }
  24. }
  25. }

答案1

得分: 2

你可以这样做,因为在你的输入文件中有一个空行。

  1. BufferedReader infile = new BufferedReader(reader);
  2. String line = "";
  3. int counter = 0;
  4. String title = "";
  5. String author = "";
  6. String price = "";
  7. try {
  8. while ((line = infile.readLine()) != null) {
  9. if (line.isEmpty())
  10. continue;
  11. ++counter;
  12. if (counter == 1) {
  13. title = line;
  14. } else if (counter == 2) {
  15. author = line;
  16. } else if (counter == 3) {
  17. price = line;
  18. SimpleBook sb = new SimpleBook(title, author, price);
  19. bookList.add(sb);
  20. counter = 0;
  21. }
  22. }
  23. } catch (IOException ex) {
  24. Logger.getLogger(SimpleBookList.class.getName()).log(Level.SEVERE, null, ex);
  25. }
英文:

you can do this as there is an blank line in your input file .

  1. BufferedReader infile = new BufferedReader(reader);
  2. String line = "";
  3. int counter = 0;
  4. String title = "";
  5. String author = "";
  6. String price = "";
  7. try {
  8. while ((line = infile.readLine()) != null) {
  9. if(line.isEmpty())
  10. continue;
  11. ++counter;
  12. if (counter == 1) {
  13. title = line;
  14. } else if (counter == 2) {
  15. author = line;
  16. } else if (counter == 3) {
  17. price = line;
  18. SimpleBook sb = new SimpleBook(title, author, price);
  19. bookList.add(sb);
  20. counter = 0;
  21. }
  22. }
  23. } catch (IOException ex) {
  24. Logger.getLogger(SimpleBookList.class.getName()).log(Level.SEVERE, null, ex);
  25. }

答案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:

确定