简体   繁体   English

是否需要将对象加载到从文本文件读取的ArrayList中并输出到JTextField?

[英]Need to load objects into an ArrayList read from a text file and output to JTextField?

I'm working on a project and I am suppose to read in the data from a text file called "stars.txt", create objects for all the sales reps in the file, load the objects onto an ArrayList and display them into my JTextField using an enhanced for loop and formatted with the toString method. 我正在研究一个项目,我想从一个名为“ stars.txt”的文本文件中读取数据,为该文件中的所有销售代表创建对象,将这些对象加载到ArrayList上并将它们显示在我的JTextField中使用增强的for循环并使用toString方法进行格式化。 I have been at this for days and cannot get the stars.txt data to display in the JTextField!!! 我已经待了好几天了,无法在JTextField中显示stars.txt数据! It compiles in NetBeans and displays the format for which I want the data in the stars.txt file to be, but not the actual data (which is salesID, firstName, lastName, osField, bsField, psField, buttonGroup1, buttonGroup2). 它在NetBeans中进行编译,并显示我希望stars.txt文件中的数据为该格式的格式,而不是实际数据(即salesID,firstName,lastName,osField,bsField,psField,buttonGroup1,buttonGroup2)的格式。 This is all under class (JButton) displayActionButtonPerformed. 这全部在(JButton)类displayActionButtonPerformed下。 I created another class called SalesReps to store the variables, sets/gets and constructor (I made this under the main class, does it matter in NetBeans I did not create a new class, ie a new TAB outside the main?) I would love any help I can get. 我创建了另一个名为SalesReps的类来存储变量,集合/获取和构造函数(我是在主类下完成的,在NetBeans中有没有关系,因为我没有创建新类,即在主类之外没有新的TAB?)我能得到的任何帮助。 Thanks. 谢谢。 Heres my code: 这是我的代码:

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

You're never parsing the lines from your file, this loop for (SalesReps aList1 : aList) is looping through an empty array and so it can't ever build the list. 您永远不会解析文件中的行for (SalesReps aList1 : aList)此循环正在遍历一个空数组,因此它永远无法构建该列表。 This code is saying here is an empty list, for everything in the empty list do this stuff (which of course never happens) then you just iterate through the file next. 这段代码说这是一个空列表,对于空列表中的所有内容都执行此操作(这当然是不会发生的),然后您只需遍历下一个文件即可。 That logic should continue while your scanner has more lines. 当扫描仪有更多行时,该逻辑应继续。 while (sc.hasNextLine()) you could do all of your parsing in this loop, by splitting up the line there. while (sc.hasNextLine())可以在此循环中进行所有解析,方法是在那儿分割行。 Also, you're only ever at most going to display one parse like this. 另外,您最多只会显示一个这样的解析。

Use StringBuilder to concatenate the data and display it in the JTextField. 使用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()); 

Hope is your help 希望是你的帮助

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM