英文:
How create a DefaultTableModel with data from text file
问题
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 test file text_file
My table view is [enter image description here][2]
1: https://i.stack.imgur.com/fG6pz.png
[2]: https://i.stack.imgur.com/6b6uP.png
I want suit each row with each column(first_row-first_column,second_row-second_column, etc..) Where is error?
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
得分: 0
文件读取问题,因为你是逐行读取而不是读取所有行。
```java
int counter = 0;
String title = "", author = "";
double price = 0.0;
while ((line = infile.readLine()) != null) {
++counter;
if (counter == 1)
title = line;
else if (counter == 2)
author = line;
else if (counter == 3) {
price = Double.parseDouble(line);
SimpleBook sb = new SimpleBook(title, author, price);
bookList.add(sb);
counter = 0;
}
}
,或者你可以查看来自Oracle文档的readAllLines方法。
<details>
<summary>英文:</summary>
The problem in reading the file, because you read line by line not read all lines
int counter = 0;
String title = "", author = "";
double price = 0.0;
while ((line = infile.readLine()) != null) {
++counter;
if (counter == 1)
title = line;
else if (counter == 2)
author = line;
else if (counter == 3) {
price = Double.parseDouble(line);
SimpleBook sb = new SimpleBook(title, author, price);
bookList.add(sb);
counter = 0;
}
}
, or you can check **readAllLines** from [Oracle doc][1]
[1]: https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/nio/file/Files.html#readAllLines(java.nio.file.Path)
</details>
# 答案2
**得分**: 0
```java
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);
}
}
}
英文:
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语言的经验与见解,让所有开发者获益!
评论