I have created a file that reads in a text file for a combo box which are exams assignments and quizes. I then have a a textfield that places the scores with the selected item in the combo box. I then add the info to a text area. I am trying to extract the grads for each of the items (quizes, assignments, etc.) so I can have the average of each item. I then need to graph this information.
this is the part of my code i am having trouble with:
/*
* To change this template, choose Tools | Templates * and open the template in the editor. */
/* * Login.java * * Created on Nov 3, 2011, 5:18:25 PM */ package assignment3;
import java.io.File; import java.io.FileOutputStream; import javax.swing.JOptionPane; import java.io.BufferedReader; import java.io.FileReader;
/** * * @author Owner */ public class MainScreen extends javax.swing.JFrame {
/** Creates new form Login */ public MainScreen() { initComponents(); try { //Read in files BufferedReader br = new BufferedReader(new FileReader("grades.txt")); //Declare String variable called line String line; //read in information off of the text file while((line=br.readLine()) !=null) { //add text to list cboxGrades.addItem(line); } } catch(Exception e) { JOptionPane.showMessageDialog(null, "File Not Found","Error",JOptionPane.ERROR_MESSAGE); System.exit(0); } } /** This method is called from within the constructor to * initialize the form. * WARNING: Do NOT modify this code. The content of this method is * always regenerated by the Form Editor. */ @SuppressWarnings("unchecked") // <editor-fold defaultstate="collapsed" desc="Generated Code"> private void initComponents() { lbltitle = new javax.swing.JLabel(); lblStudentID = new javax.swing.JLabel(); txtStudentID = new javax.swing.JTextField(); lblStudentName = new javax.swing.JLabel(); txtStudentName = new javax.swing.JTextField(); cboxGrades = new javax.swing.JComboBox(); lblGrades = new javax.swing.JLabel(); txtGrades = new javax.swing.JTextField(); btnSave = new javax.swing.JButton(); btnClear = new javax.swing.JButton(); btnGraph = new javax.swing.JButton(); jScrollPane1 = new javax.swing.JScrollPane(); areaGrades = new javax.swing.JTextArea(); setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE); lbltitle.setFont(new java.awt.Font("Tahoma", 1, 14)); lbltitle.setText("Main Screen"); lblStudentID.setText("Studen ID"); txtStudentID.setColumns(5); txtStudentID.setToolTipText("Enter Student Name"); txtStudentID.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { txtStudentIDActionPerformed(evt); } }); lblStudentName.setText("Student Name"); txtStudentName.setColumns(5); txtStudentName.setToolTipText("Enter Student name"); txtStudentName.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { txtStudentNameActionPerformed(evt); } }); cboxGrades.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { cboxGradesActionPerformed(evt); } }); lblGrades.setText("Grades"); txtGrades.setToolTipText("Enter in Assignments"); txtGrades.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { txtGradesActionPerformed(evt); } }); txtGrades.addKeyListener(new java.awt.event.KeyAdapter() { public void keyPressed(java.awt.event.KeyEvent evt) { txtGradesKeyPressed(evt); } }); btnSave.setText("Save"); btnSave.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { btnSaveActionPerformed(evt); } }); btnClear.setText("Clear"); btnClear.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { btnClearActionPerformed(evt); } }); btnGraph.setText("Graph"); btnGraph.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { btnGraphActionPerformed(evt); } }); areaGrades.setColumns(20); areaGrades.setRows(5); jScrollPane1.setViewportView(areaGrades); javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane()); getContentPane().setLayout(layout); layout.setHorizontalGroup( layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup() .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup() .addGap(53, 53, 53) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addComponent(lblStudentID) .addGroup(layout.createSequentialGroup() .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING) .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addComponent(txtStudentID, javax.swing.GroupLayout.PREFERRED_SIZE, 57, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(lblStudentName) .addComponent(lblGrades) .addComponent(txtStudentName, javax.swing.GroupLayout.DEFAULT_SIZE, 117, Short.MAX_VALUE)) .addComponent(btnSave)) .addComponent(cboxGrades, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.PREFERRED_SIZE, 117, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(txtGrades, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.PREFERRED_SIZE, 40, javax.swing.GroupLayout.PREFERRED_SIZE)) .addGap(66, 66, 66) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING) .addGroup(layout.createSequentialGroup() .addComponent(btnGraph) .addGap(100, 100, 100) .addComponent(btnClear)) .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)) .addGap(77, 77, 77)))) .addGroup(layout.createSequentialGroup() .addGap(216, 216, 216) .addComponent(lbltitle))) .addContainerGap()) ); layout.setVerticalGroup( layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup() .addComponent(lbltitle) .addGap(23, 23, 23) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING) .addGroup(layout.createSequentialGroup() .addComponent(lblStudentID) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(txtStudentID, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(lblStudentName) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(txtStudentName, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED) .addComponent(lblGrades) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED) .addComponent(cboxGrades, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)) .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)) .addGap(18, 18, 18) .addComponent(txtGrades, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addGap(18, 18, 18) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(btnSave) .addComponent(btnClear, javax.swing.GroupLayout.PREFERRED_SIZE, 23, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(btnGraph)) .addContainerGap(95, Short.MAX_VALUE)) ); pack(); }// </editor-fold> private void txtStudentIDActionPerformed(java.awt.event.ActionEvent evt) { // TODO add your handling code here: if (txtStudentID.getText().contentEquals(" ")) { //Error message to show that Student ID is empty JOptionPane.showMessageDialog(this, "You did not enter an ID", "Error", JOptionPane.ERROR_MESSAGE); } if (txtStudentID.getText().length() > 5) { //Error message for Student ID being longer than 5 JOptionPane.showMessageDialog(this, "Your ID is too long!", "Error", JOptionPane.ERROR_MESSAGE); } //Checks to see if Student ID is empty try { //Changes student ID to an Integer int number = Integer.parseInt(this.txtStudentID.getText()); } //catches exception for non valid number catch (Exception e) { //error message to show numbers only JOptionPane.showMessageDialog(this, "Please enter in numbers only", "Error", JOptionPane.ERROR_MESSAGE); txtStudentID.setText(null); } } private void txtStudentNameActionPerformed(java.awt.event.ActionEvent evt) { // TODO add your handling code here: if(txtStudentName.getText() == null) { JOptionPane.showMessageDialog(null, "Invalid name","Error Message",JOptionPane.ERROR_MESSAGE); } } private void txtGradesActionPerformed(java.awt.event.ActionEvent evt) { // TODO add your handling code here: int scores = Integer.parseInt(txtGrades.getText()); if(scores > 100) { JOptionPane.showMessageDialog(null, "Invalid Grade, try again","Error",JOptionPane.ERROR_MESSAGE); } String list = cboxGrades.getSelectedItem()+": "+txtGrades.getText()+"\n"; areaGrades.append(list); txtGrades.setText(null); if(cboxGrades.getSelectedItem().equals("Exam 1")) cboxGrades.removeItemAt(1); if(cboxGrades.getSelectedItem().equals("Exam 2")) cboxGrades.removeItemAt(1); if(cboxGrades.getSelectedItem().equals("Exam 3")) cboxGrades.removeItemAt(1); if(cboxGrades.getSelectedItem().equals("Assignment 1")) cboxGrades.removeItemAt(1); if(cboxGrades.getSelectedItem().equals("Assignment 2")) cboxGrades.removeItemAt(1); if(cboxGrades.getSelectedItem().equals("Assignment 3")) cboxGrades.removeItemAt(1); if(cboxGrades.getSelectedItem().equals("Quiz 1")) cboxGrades.removeItemAt(1); if(cboxGrades.getSelectedItem().equals("Quiz 2")) cboxGrades.removeItemAt(1); if(cboxGrades.getSelectedItem().equals("Quiz 3")) cboxGrades.setSelectedItem(1); if(cboxGrades.getSelectedItem().equals("Final Exam")) cboxGrades.removeItemAt(1); } private void btnSaveActionPerformed(java.awt.event.ActionEvent evt) { // TODO add your handling code here: try { String content = new String(); content = txtStudentID.getText().toString()+" "+txtStudentName.getText().toString()+ " "+areaGrades.getText().toString(); File file = new File("Student.txt"); if(!file.exists()) { file.createNewFile(); } FileOutputStream fop = new FileOutputStream(file); fop.write(content.getBytes()); fop.flush(); fop.close(); JOptionPane.showMessageDialog(null, "Data was added to the file!! "+file); } catch(Exception e) { JOptionPane.showMessageDialog(null, "The Data was not added to the file!","Error",JOptionPane.ERROR_MESSAGE); } } private void btnClearActionPerformed(java.awt.event.ActionEvent evt) { // TODO add your handling code here: txtStudentID.setText(null); txtStudentName.setText(null); txtGrades.setText(null); areaGrades.setText(null); try { //Read in files BufferedReader br = new BufferedReader(new FileReader("grades.txt")); //Declare String variable called line String line; //read in information off of the text file while((line=br.readLine()) !=null) { //add text to list cboxGrades.addItem(line); } } catch(Exception e) { JOptionPane.showMessageDialog(null, "File Not Found","Error",JOptionPane.ERROR_MESSAGE); System.exit(0); } } private void cboxGradesActionPerformed(java.awt.event.ActionEvent evt) { // TODO add your handling code here } private void btnGraphActionPerformed(java.awt.event.ActionEvent evt) { // TODO add your handling code here: System.out.println("Index\tValue"); String myarray[] = null; for(int i = 0; i < myarray.length;i++) { myarray[] = areaGrades.getText().toString(); } } private void txtGradesKeyPressed(java.awt.event.KeyEvent evt) { // TODO add your handling code here: } /** * @param args the command line arguments */ public static void main(String args[]) { /* Set the Nimbus look and feel */ //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) "> /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel. * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html */ try { for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) { if ("Nimbus".equals(info.getName())) { javax.swing.UIManager.setLookAndFeel(info.getClassName()); break; } } } catch (ClassNotFoundException ex) { java.util.logging.Logger.getLogger(MainScreen.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } catch (InstantiationException ex) { java.util.logging.Logger.getLogger(MainScreen.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } catch (IllegalAccessException ex) { java.util.logging.Logger.getLogger(MainScreen.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } catch (javax.swing.UnsupportedLookAndFeelException ex) { java.util.logging.Logger.getLogger(MainScreen.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } //</editor-fold> /* Create and display the form */ java.awt.EventQueue.invokeLater(new Runnable() { @Override public void run() { new MainScreen().setVisible(true); } }); } // Variables declaration - do not modify private javax.swing.JTextArea areaGrades; private javax.swing.JButton btnClear; private javax.swing.JButton btnGraph; private javax.swing.JButton btnSave; private javax.swing.JComboBox cboxGrades; private javax.swing.JScrollPane jScrollPane1; private javax.swing.JLabel lblGrades; private javax.swing.JLabel lblStudentID; private javax.swing.JLabel lblStudentName; private javax.swing.JLabel lbltitle; private javax.swing.JTextField txtGrades; private javax.swing.JTextField txtStudentID; private javax.swing.JTextField txtStudentName; // End of variables declaration
}