Average

Average

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

}

View Answers

Ads









Related Tutorials/Questions & Answers:
Average
Average   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..., assignments, etc.) so I can have the average of each item. I then need to graph
Student average
to maintain this status is to maintain an average not below 1.8. write a java program... should display the average grade of the student and would display the message...: 1.2 Enter number of units: 1 you got an average of 1.59 congratulation
Advertisements
Average Age of my Class?
Average Age of my Class?  average age of my class
calculate average
and write a program and calculate your final average in this course. The details
ModuleNotFoundError: No module named 'average'
ModuleNotFoundError: No module named 'average'  Hi, My Python... 'average' How to remove the ModuleNotFoundError: No module named 'average... to install padas library. You can install average python with following command
sum and average of grades
sum and average of grades  how to print a program which is sum and average of Korean (90), English(85), Mathematics(78), Biology(65), Chemistry(83
Hibernate criteria average Example
Hibernate criteria average Example   How to find the average in Hibernate? Thanks   Example of finding average on a filed in Hibernate using criteria. For this you have to use Projections.avg() in your program. Check
compute the average of degrees
compute the average of degrees  using c++ language , write a program to do the following 1 enter three degrees of a student by the user . 2 compute the average of his degrees
How to calculate the average in Hibernate?
How to calculate the average in Hibernate?  Hi, I have to calculate the average in Hibernate. How to calculate the average in Hibernate? Thanks... of calculating the average on the fee field in database: Projections.avg("fee
SQL Average
SQL Average       SQL Average, the part of Aggregate Function. SQL Average is used to compute average value of the records in a table of the database. Understand
Calculate an average Age of my Class?
Calculate an average Age of my Class?  average age of my class
Calculate sum and Average in java
Calculate sum and Average in java  How to calculate sum and average.... Description:- In this example we are calculating sum and average of n numbers... the sum by the total number of numbers, we have determined the average of numbers
Average of Array
Average of Array       In this section, we will learn how to get an average of array. For this, first... that calculates the average of array (result/nums.length). And finally it will display
ModuleNotFoundError: No module named 'average-pixels'
ModuleNotFoundError: No module named 'average-pixels'  Hi, My... named 'average-pixels' How to remove the ModuleNotFoundError: No module named 'average-pixels' error? Thanks   Hi, In your python
ModuleNotFoundError: No module named 'average-pixels'
ModuleNotFoundError: No module named 'average-pixels'  Hi, My... named 'average-pixels' How to remove the ModuleNotFoundError: No module named 'average-pixels' error? Thanks   Hi, In your python
ModuleNotFoundError: No module named 'Dice-Average'
ModuleNotFoundError: No module named 'Dice-Average'  Hi, My Python... 'Dice-Average' How to remove the ModuleNotFoundError: No module named 'Dice-Average' error? Thanks   Hi, In your python
ModuleNotFoundError: No module named 'django-average'
ModuleNotFoundError: No module named 'django-average'  Hi, My... named 'django-average' How to remove the ModuleNotFoundError: No module named 'django-average' error? Thanks   Hi, In your python
ModuleNotFoundError: No module named 'django-average'
ModuleNotFoundError: No module named 'django-average'  Hi, My... named 'django-average' How to remove the ModuleNotFoundError: No module named 'django-average' error? Thanks   Hi, In your python
MySQL Average Command
MySQL Average Command This example illustrates how to execute the Average command in MySQL. In this example we create a select query to find the average of 'lastAccess' field.   Query  
How to calculate average of array in Java?
How to calculate average of array in Java?  Hi, I have an integer array and I want to calculate the average of the values in it. How to calculate average of array in Java? Thanks   Hi, You can iterate through
array average across column
array average across column  How can I find the overall averages of the columns in this array? when i run my code it gives me an output that looks like this (showing me wrong averages): How can I find the overall averages
Finding Average in JSP
Finding Average in JSP          In mathematics, an average is a measure of the middle of the data set. The average value is the total amount divided by the total
SQL Average Count
SQL Average Count       Average Count in SQL is used to count the aggregate sum of any field... example ,which helps you to calculate the average count of any records specified
ModuleNotFoundError: No module named 'average-decision-ordering'
ModuleNotFoundError: No module named 'average-decision-ordering'  Hi...: No module named 'average-decision-ordering' How to remove the ModuleNotFoundError: No module named 'average-decision-ordering' error? Thanks  
ModuleNotFoundError: No module named 'average-n-numbers-calvine'
ModuleNotFoundError: No module named 'average-n-numbers-calvine'  Hi...: No module named 'average-n-numbers-calvine' How to remove the ModuleNotFoundError: No module named 'average-n-numbers-calvine' error? Thanks  
ModuleNotFoundError: No module named 'average-decision-ordering'
ModuleNotFoundError: No module named 'average-decision-ordering'  Hi...: No module named 'average-decision-ordering' How to remove the ModuleNotFoundError: No module named 'average-decision-ordering' error? Thanks  
ModuleNotFoundError: No module named 'average-n-numbers-calvine'
ModuleNotFoundError: No module named 'average-n-numbers-calvine'  Hi...: No module named 'average-n-numbers-calvine' How to remove the ModuleNotFoundError: No module named 'average-n-numbers-calvine' error? Thanks  
Hibernate Criteria average example
Hibernate Criteria average example - Learn how to perform Hibernate Criteria Average Query Here we will use the avg() method of org.hibernate.criterion.Projections factory for getting the Projection instance for performing the average
Highest average score among 25 students
Highest average score among 25 students  a program that prompts... again the grade) calculate average per student and will calculate the highest average among students who should be printed. how can i solve this problem
SQL Average Count
SQL Average Count       Average Count in SQL is used to count the aggregate sum of any field... demonstrative example ,which helps you to calculate the average count of any
identify the order of the average running time of the method
identify the order of the average running time of the method  public static double getMedian (double[] A) { // given an ordered array of length n, return the median int n = A.length; if ((n % 2) == 1) return A[n/2]; else

Ads