我正在处理一个项目,我想从一个名为" stars.txt"的文本文件中读取数据,为文件中的所有销售代表创建对象,加载将对象放到ArrayList上,并使用增强的for循环将它们显示到我的JTextField中,并使用toString方法进行格式化。我已经在这几天了,无法获得在JTextField中显示的stars.txt数据!它在NetBeans中编译并显示我想要stars.txt文件中的数据的格式,但不显示实际数据(即salesID,firstName,lastName,osField,bsField,psField,buttonGroup1,buttonGroup2)。这都属于类(JButton)displayActionButtonPerformed。我创建了另一个名为SalesReps的类来存储变量,集合/获取和构造函数(我在主类下创建它,在NetBeans中是否重要,我没有创建新类,即主要的新TAB?)我很想我能得到任何帮助。谢谢。继承我的代码:
private void displayButtonActionPerformed(java.awt.event.ActionEvent evt) {
File file = new File("stars.txt");
SalesReps rep = new SalesReps();
ArrayList<SalesReps> aList = new ArrayList<>();
try {
Scanner sc = new Scanner(file);
int sID = 0;
String fName = null;
String lName = null;
double oSupp = 0;
double bSupp = 0;
double pSupp = 0;
String button1 = null;
String button2 = null;
//SalesReps rep = null;
for (SalesReps aList1 : aList) {
System.out.println(aList1);
sID = sc.nextInt();
fName = sc.next();
lName = sc.next();
oSupp = sc.nextDouble();
bSupp = sc.nextDouble();
pSupp = sc.nextDouble();
button1 = sc.next();
button2 = sc.next();
rep = new SalesReps(sID, fName, lName, oSupp, bSupp, pSupp,
button1, button2);
aList.add(rep);
}
while (sc.hasNextLine()) {
String line = sc.nextLine();
System.out.println(line);
}
sc.close();
} catch (FileNotFoundException e) {
}
resultsTextArea.append(rep.toString());
}
public class SalesReps {
private int salesID = 0;
private String firstName = null;
private String lastName= null ;
private double osField = 0;
private double bsField = 0;
private double psField = 0;
private String buttonGroup1 = null;
private String buttonGroup2 = null;
public int getSalesID() {
return salesID;
}
public void setSalesID(int sID) {
salesID = sID;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String fName) {
firstName = fName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lName) {
lastName = lName;
}
public double getOsField() {
return osField;
}
public void setOsField(double oSupp) {
osField = oSupp;
}
public double getBsField() {
return bsField;
}
public void setBsField(double bSupp) {
bsField = bSupp;
}
public double getPsField() {
return psField;
}
public void setPsField(double pSupp) {
psField = pSupp;
}
public String getButtonGroup1() {
return buttonGroup1;
}
public void setButtonGroup1(String button1) {
buttonGroup1 = button1;
}
public String getButtonGroup2() {
return buttonGroup2;
}
public void setButtonGroup2(String button2) {
buttonGroup2 = button2;
}
public SalesReps(int sID, String fName, String lName,
double oSupp, double bSupp, double pSupp, String button1,
String button2){
}
public SalesReps() {
}
@Override
public String toString(){
return String.format(" %s %s %s\n SUPPLIES"
+ " %.2f \n BOOKS %.2f \n PAPER %.2f \n %s \n %s \n",salesID,
firstName, lastName, osField, bsField, psField,
buttonGroup1, buttonGroup2);
}
}
答案 0 :(得分:0)
您永远不会解析文件中的行,此循环for (SalesReps aList1 : aList)
循环遍历空数组,因此无法构建列表。这段代码在这里说是一个空列表,对于空列表中的所有东西都做这个东西(当然从未发生过)然后你只是遍历文件。当您的扫描仪有更多行时,该逻辑应该继续。 while (sc.hasNextLine())
你可以在这个循环中完成所有的解析,通过在那里分割线。此外,您最多只会像这样显示一个解析。
答案 1 :(得分:0)
使用StringBuilder连接数据并在JTextField中显示它。
StringBuilder s = new StringBuilder();
.
.
.
for (SalesReps aList1 : aList) {
System.out.println(aList1);
sID = sc.nextInt();
fName = sc.next();
lName = sc.next();
oSupp = sc.nextDouble();
bSupp = sc.nextDouble();
pSupp = sc.nextDouble();
button1 = sc.next();
button2 = sc.next();
rep = new SalesReps(sID, fName, lName, oSupp, bSupp, pSupp,
button1, button2);
aList.add(rep);
s.append(rep.toString());
}
.
.
.
resultsTextArea.append(s.toString());
希望是你的帮助