gpt4 book ai didi

java - 如何使用文本文件中的数据创建 DefaultTableModel

转载 作者:行者123 更新时间:2023-12-01 16:28:47 25 4
gpt4 key购买 nike

我有这个测试文件 text_file我的 TableView 是 enter image description here我想将每一行与每一列相匹配(第一行-第一列,第二行-第二列,等等。)错误在哪里?

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

}

最佳答案

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

}

关于java - 如何使用文本文件中的数据创建 DefaultTableModel,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62090905/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com