I am trying to write a code for the following:
Write a Swing program that declares an empty array of grades with a maximum of 50. Implement a JOptionPane input box within a while loop to allow the user to enter the grades using a -1 as a sentinel value. After the grades are entered, a content pane should display the grades sorted from lowest to highest. Write a loop that goes through the array looking for elements that are greater than 0. Keep a running counter of those elements and accumulate them into a grand total. Divide the grand total by the total number of grades entered to find an average. Display the average in the JTextPane, and use the DecimalFormat method to round the average.
The code I wrote so far is working however when I enter grades it doesn't work. Please help! Here is the code:

/*
	Chapter 7:	Programming Assignment 4
	Programmer:	T. du Preez
	Date:		September 12, 2009
	Filename:	AverageGradesss.java
	Purpose:	Calculates Average grades
*/

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.text.*;
import java.text.DecimalFormat;
import java.util.Arrays;

public class AverageGrades extends JFrame implements ActionListener
{
    //construct components
    JTextPane textPane = new JTextPane();

    //initialize data in arrays
    int[] grades = new int[50];

    //contruct instance of Average Grade
    public AverageGrades()
    {
        super("Average Grade");
    }
    //create Menu System
    public JMenuBar createMenuBar()
    {
        //create an instance of the menubar
        JMenuBar mnuBar = new JMenuBar();
        setJMenuBar(mnuBar);

        //construct and populate the File menu
        JMenu mnuFile = new JMenu("File", true);
        mnuFile.setMnemonic(KeyEvent.VK_F);
        mnuFile.setDisplayedMnemonicIndex(0);
        mnuBar.add(mnuFile);

        //adding items to the File Menu
        JMenuItem mnuFileExit = new JMenuItem("Exit");
        mnuFileExit.setMnemonic(KeyEvent.VK_X);
        mnuFileExit.setDisplayedMnemonicIndex(1);
        mnuFile.add(mnuFileExit);
        mnuFileExit.setActionCommand("Exit");
        mnuFileExit.addActionListener(this);

        //construct and populate the Edit menu
        JMenu mnuEdit = new  JMenu("Edit", true);
        mnuEdit.setMnemonic(KeyEvent.VK_E);
        mnuEdit.setDisplayedMnemonicIndex(0);
        mnuBar.add(mnuEdit);

        //adding items to Edit Menu
        JMenuItem mnuEditEnter = new JMenuItem("Enter Grades");
        mnuEditEnter.setMnemonic(KeyEvent.VK_G);
        mnuEditEnter.setDisplayedMnemonicIndex(0);
        mnuEdit.add(mnuEditEnter);
        mnuEditEnter.setActionCommand("Enter");
        mnuEditEnter.addActionListener(this);

        return mnuBar;
    }

    //create the content pane
    public Container createContentPane()
    {

    //create the JTextPane and center panel
    JPanel centerPanel = new JPanel();
    textPane = addTextToTextPane();
    JScrollPane scrollPane = new JScrollPane(textPane);

    scrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);

    scrollPane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
    scrollPane.setPreferredSize(new Dimension(900, 200));
    centerPanel.add(scrollPane);

    //create Container and set attributes
    Container c = getContentPane();
    c.setLayout(new BorderLayout(10, 10));
    c.add(centerPanel, BorderLayout.CENTER);

    return c;
    }
    //method to set fontstyles
    protected void setTabAndStyles(JTextPane textPane)
    {
        //create TabStop
        TabStop[] tabs = new TabStop[1];
            tabs[0] = new TabStop(200,TabStop.ALIGN_LEFT, TabStop.LEAD_NONE);
        TabSet tabset = new TabSet(tabs);

        //set Tab Style
        StyleContext tabStyle = StyleContext.getDefaultStyleContext();
        AttributeSet aset = tabStyle.addAttribute(SimpleAttributeSet.EMPTY, StyleConstants.TabSet, tabset);
        textPane.setParagraphAttributes(aset,false);

        //set Font Styles
        Style fontStyle = StyleContext.getDefaultStyleContext().getStyle(StyleContext.DEFAULT_STYLE);
        Style regular = textPane.addStyle("regular", fontStyle);
        StyleConstants.setFontFamily(fontStyle, "SansSerif");

        Style s = textPane.addStyle("bold", regular);
        StyleConstants.setBold(s,true);

        s= textPane.addStyle("italic", regular);
        StyleConstants.setItalic(s,true);

        s= textPane.addStyle("medium", regular);
        StyleConstants.setFontSize(s,24);
        textPane.setEditable(false);
    }
    //method to add new text to the JTextPane
    public JTextPane addTextToTextPane()
    {
        Document doc = textPane.getDocument();
        try
        {
            //crear previos text
            doc.remove(0,doc.getLength());
            //insert title
            doc.insertString(0,"\t\t\t\t\t\t\t\t\t\tGRADE LIST", textPane.getStyle("medium"));

            //insert detail
            for(int j=0; j<grades.length; j++)
            {
                doc.insertString(doc.getLength(), grades[j] + "\t", textPane.getStyle("medium"));
            }
        }
        catch(BadLocationException ble)
        {
            System.err.println("Couldn't insert text.");
        }
        return textPane;
    }
    //event to process user clicks
    public void actionPerformed(ActionEvent e)
    {
        String arg = e.getActionCommand();

        //user clicks Exit on File menu
        if(arg == "Exit")
        System.exit(0);

        //user clicks enter Grades in the Edit Menu
        if(arg == "Enter")
        {
            String gradeString = JOptionPane.showInputDialog("Please enter your new Grade");
            int newGrades = Integer.parseInt(gradeString);


            //enlarge arrays
            grades = enlargeArray(grades);

            //add new grades to the array
            grades[grades.length-1] = newGrades;

            //call sort method
            Arrays.sort(grades);
        }
    }
    //method to enlarge the array by 1
    public int[] enlargeArray(int[] currentArray)
    {
        int[] newArray = new int[currentArray.length +1];
        for(int i=0; i<currentArray.length; i++)
        newArray[i] = currentArray[i];
        return newArray;
    }

    //method to sort array
    public void grades(int tempArray[])
    {
        //loop to control number of passes
        for(int pass = 1; pass<tempArray.length; pass++)
        {
            for(int element = 0; element<tempArray.length-1; element++)
            {
                swap(grades, element, element + 1);
            }
        }
        addTextToTextPane();
    }
    //method to swap elements of an array
    public void swap(int swapArray[], int first, int second)
    {
        int hold;
        hold = swapArray[first];
        swapArray[first] = swapArray[second];
        swapArray[second] = hold;

    }
    //main method
    public static void main(String args[])
    {
        JFrame.setDefaultLookAndFeelDecorated(true);
        AverageGrades f = new AverageGrades();
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.setJMenuBar(f.createMenuBar());
        f.setContentPane(f.createContentPane());
        f.setSize(300,200);
        f.setVisible(true);
    }

}//end of the main class

Line 162

//call sort method
            Arrays.sort(grades);
            System.out.println("current size of grades array is " + grades.length);
            // grades(grades);
            addTextToTextPane();

missing addText

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.