Need help with assignment
So, I've done around 80 to 90% of this latest assignment but I am having trouble on the last part of the assignment. I know the code is long, bare with me.
So, the assignment is to make a GUI window that has two buttons. One button opens a file named studentnames.txt which includes this data (an ID field and a Name field):
3305 Smith Henry
5555 Eddy Olivia
8915 Johnson Luke
While the second button grabs a file named studentscores.txt (an ID field and a grades field):
3305 92.0
5555 95.5
8915 98.5
3305 89.0
5555 90.5
8915 95.5
3305 78.5
5555 85.0
8915 82.0
In the previous assignment - we used objects, classes and methods to grab the input from both files, sort the ID to the name and then sort the grades to the ID and then finally, print out the data. Now, we are redesigning this code to print it out via a GUI.
I'm having a difficult time with the output section of the GUI. When the data is parsed into the GUI - it will output it to a text field in the middle of the GUI. Would you mind looking at my code and pointing me in the right direction? What am I missing? What am I lacking? I've attached my code and also is sending a link for pastebin for my code.
Thanks in advance!
Pastebin: P_Supp_11 - Pastebin.com
Code :
/*Filename: P_Supplemental_11.java
* Created: November 7, 2012
* Author: Dustin Hunt
* Purpose: Create a form using a border layout with a FileChooser and two buttons*/
package p_supplemental_11;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.*;
import javax.swing.*;
import java.util.*;
import java.util.logging.Level;
import java.util.logging.Logger;
public class P_Supplemental_11 extends JFrame {
JPanel jpnl1 = new JPanel();
JPanel southPanel = new JPanel();
JButton jbtReadFile1 = new JButton("Get Student Names");
JButton jbtReadFile2 = new JButton("Get Student Grades");
JTextField jtxtFilePath = new JTextField();
JLabel jlblDesc = new JLabel("Click a button to open each file!");
JTextArea jtxtAfileContents = new JTextArea();
P_Supplemental_11() {
this.setLayout(new BorderLayout(5, 10));
jpnl1.setLayout(new GridLayout(2, 2));
jpnl1.add(jlblDesc);
//jpnl1.add(jtxtFilePath);
southPanel.add(jbtReadFile1);
southPanel.add(jbtReadFile2);
add(southPanel, BorderLayout.SOUTH);
add(jpnl1, BorderLayout.NORTH);
add(jtxtAfileContents, BorderLayout.CENTER);
jbtReadFile1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
jbtReadFileActionPerformed(evt);
}
});
jbtReadFile2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
jbtReadFileActionPerformed2(evt);
}
});
} // end constructor
class Student {
private int stuId;
private String stuName;
private ArrayList<Double> grades;
Student(int idIn, String nameIn) {
this.stuId = idIn;
this.stuName = nameIn;
} // end student class
public int getStuId() {
return stuId;
}
public void setStuId(int stuId) {
this.stuId = stuId;
}
String getStuName() {
return stuName;
}
public void setStuName(String stuName) {
this.stuName = stuName;
}
public ArrayList<Double> getGrades() {
return grades;
}
public void setGrades(ArrayList grades) {
this.grades = grades;
}
} // end class Student
ArrayList<Student> students = new ArrayList();
private void jbtReadFileActionPerformed(ActionEvent evt) {
try {
JFileChooser fileChooser = new JFileChooser();
if (fileChooser.showOpenDialog(null) == JFileChooser.APPROVE_OPTION) {
java.io.File file = fileChooser.getSelectedFile();
Scanner input = new Scanner(file);
StringBuilder output = new StringBuilder();
while(input.hasNext()) {
students.add(new Student(input.nextInt(),input.nextLine()));
} // end while
jtxtAfileContents.setText(output.toString());
input.close();
} // end if
}// end action method for jbtReadFile button
catch (FileNotFoundException ex) {
Logger.getLogger(P_Supplemental_11.class.getName()).log(Level.SEVERE, null, ex);
}
}
private void jbtReadFileActionPerformed2(ActionEvent evt) {
try {
JFileChooser fileChooser = new JFileChooser();
if (fileChooser.showOpenDialog(null) == JFileChooser.APPROVE_OPTION) {
java.io.File file = fileChooser.getSelectedFile();
Scanner input = new Scanner(file);
while (input.hasNext()) {
students.add(new Student(input.nextInt(), input.nextLine()));
} // end while
for (int o = 0; o < students.size(); o++) {
students.get(o).setGrades(collectGrades(students.get(o).getStuId()));
StringBuilder output = new StringBuilder();
output.append(students.get(o).getStuId());
output.append(students.get(o).getStuName());
output.append(averageGrade(students.get(o).getGrades()));
jtxtAfileContents.setText(output.toString());
}
} // end for
} // end action method for jbtReadFile button
catch (FileNotFoundException ex) {
Logger.getLogger(P_Supplemental_11.class.getName()).log(Level.SEVERE, null, ex);
}
}
public static ArrayList<Double> collectGrades(int inId) {
ArrayList<Double> outGrade = new ArrayList();
try {
File inFile = new File("c:/temp/studentScores.txt");
Scanner input = new Scanner(inFile);
while (input.hasNext()) {
int tmpInt = input.nextInt();
double tmpDbl = input.nextDouble();
if (tmpInt == inId) {
outGrade.add(tmpDbl);
} // end if
} // end while
} catch (FileNotFoundException ex) {
Logger.getLogger(P_Supplemental_11.class.getName()).log(Level.SEVERE, null, ex);
} // end catch
return outGrade;
} // end method
public static double averageGrade(ArrayList<Double> gradeIn) {
double outAvg = 0.0;
for (int y = 0; y < gradeIn.size(); y++) {
outAvg += gradeIn.get(y);
} // end for
return outAvg / gradeIn.size();
}
public static void main(String[] args) {
P_Supplemental_11 frame = new P_Supplemental_11();
frame.setTitle("P_Supplemenetal_10");
frame.setSize(410, 520);
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
} // end main
} // end public class P_Supp_11
Re: Need help with assignment
Quote:
I'm having a difficult time with the output section of the GUI.
Can you explain what the problem is? What is wrong with the way the program executes now?
Re: Need help with assignment
Sorry I was a little vague - the assignment is just a little long.
In the previous assignment, we didn't use a GUI but just printf to print out the data. The currentGrades method and averageGrade method should add the grades to the proper ID and print out the average of those grades. So, I have all the methods and proper code to execute the program - I'm just having a hard time printing it out to the middle text box.
I know I need to output my data through a StringBuilder but I'm unfamiliar with this process.
So, to put it simply -- I'm having a hard time getting my output to display correctly through the center text box. Right now, I am getting some output but it is incorrect. Would you like me to send you the file of the last assignment to show you what it should be doing?
Re: Need help with assignment
Quote:
I am getting some output but it is incorrect.
Can you explain what you get and describe what you want the output to be?
When I execute the code I get this in the upper left corner of the textarea:
8915 82.092.0
Some debugging suggestions so you can see what the program is doing:
1) Add a toString() method to the Student class that returns a String with the name, id and grades.
2) Print out the contents of the students ArrayList after every time you add items to it