如何在 java swing 中将数据从文本文件加载到 Jlist?
How can i load data from text file to Jlist in java swing?
我正在学习 java。当我单击 JButton 将数据从文本文件加载到 JList 时遇到问题,但它注意到错误 "java.lang.NumberFormatException: For input string: """。首先,我将数据从 4 JTextField 输入到 JList,然后保存文件文本。我的 txt 文件包含内容:1-java-3-4。请有人帮助我。谢谢。这是我的代码按钮保存和加载数据:
private void btAddBookActionPerformed(java.awt.event.ActionEvent evt) {
String BookId = txtBookID.getText();
String BookName = txtBookName.getText();
String Quantity = txtQuantity.getText();
String Price = txtPrice.getText();
if(BookId.equals("")|| BookId.equalsIgnoreCase("Type Here") || BookName.equals("")|| BookName.equalsIgnoreCase("Type Here") || Quantity.equals("")|| Quantity.equalsIgnoreCase("Type Here")||Price.equals("")|| Price.equalsIgnoreCase("Type Here")){
txtBookID.setText("Type Here");
txtBookName.setText("Type Here");
txtPrice.setText("Type Here");
txtQuantity.setText("Type Here");
}else{
listmodel.addElement(BookId+"-"+BookName+"-"+Quantity+"-
"+Price);
booklist.setModel(listmodel);
txtBookID.setText("");
txtBookName.setText("");
txtQuantity.setText("");
txtPrice.setText("");
}
private void btLoadDBActionPerformed(java.awt.event.ActionEvent evt) {
BufferedReader br = null;
try{
br = new BufferedReader(new FileReader("BookList.txt"));
int val = Integer.parseInt(br.readLine());
for (int i = 0; i < val; i++) {
String ss = br.readLine();
listmodel.addElement(ss);
}
booklist.setModel(listmodel);
}
catch(Exception e){
System.out.println(""+e);
}
finally{
try{
br.close();
}
catch(Exception e){
System.out.println(""+e);
}
}
}
仔细查看您的代码,您似乎根本不需要整数解析。问题是您以错误的方式读取文件。代替 for
循环,逐行读取文本文件(当您不知道文件的确切大小时)的常见习惯用法是使用 while
循环,如下所示:
br = new BufferedReader(new FileReader("BookList.txt"));
String line;
while ((line = br.readLine()) != null) {
listmodel.addElement(line);
}
booklist.setModel(listmodel);
我正在学习 java。当我单击 JButton 将数据从文本文件加载到 JList 时遇到问题,但它注意到错误 "java.lang.NumberFormatException: For input string: """。首先,我将数据从 4 JTextField 输入到 JList,然后保存文件文本。我的 txt 文件包含内容:1-java-3-4。请有人帮助我。谢谢。这是我的代码按钮保存和加载数据:
private void btAddBookActionPerformed(java.awt.event.ActionEvent evt) {
String BookId = txtBookID.getText();
String BookName = txtBookName.getText();
String Quantity = txtQuantity.getText();
String Price = txtPrice.getText();
if(BookId.equals("")|| BookId.equalsIgnoreCase("Type Here") || BookName.equals("")|| BookName.equalsIgnoreCase("Type Here") || Quantity.equals("")|| Quantity.equalsIgnoreCase("Type Here")||Price.equals("")|| Price.equalsIgnoreCase("Type Here")){
txtBookID.setText("Type Here");
txtBookName.setText("Type Here");
txtPrice.setText("Type Here");
txtQuantity.setText("Type Here");
}else{
listmodel.addElement(BookId+"-"+BookName+"-"+Quantity+"-
"+Price);
booklist.setModel(listmodel);
txtBookID.setText("");
txtBookName.setText("");
txtQuantity.setText("");
txtPrice.setText("");
}
private void btLoadDBActionPerformed(java.awt.event.ActionEvent evt) {
BufferedReader br = null;
try{
br = new BufferedReader(new FileReader("BookList.txt"));
int val = Integer.parseInt(br.readLine());
for (int i = 0; i < val; i++) {
String ss = br.readLine();
listmodel.addElement(ss);
}
booklist.setModel(listmodel);
}
catch(Exception e){
System.out.println(""+e);
}
finally{
try{
br.close();
}
catch(Exception e){
System.out.println(""+e);
}
}
}
仔细查看您的代码,您似乎根本不需要整数解析。问题是您以错误的方式读取文件。代替 for
循环,逐行读取文本文件(当您不知道文件的确切大小时)的常见习惯用法是使用 while
循环,如下所示:
br = new BufferedReader(new FileReader("BookList.txt"));
String line;
while ((line = br.readLine()) != null) {
listmodel.addElement(line);
}
booklist.setModel(listmodel);