Okay, I have read through all of the average grade java swing threads I could find on this site, but I can't seem to fix this problem. Everything seems to work correctly, but the average grade won't display in the JTextPane. I am going to get rid of the extra spaces tomorrow, but I don't think they are the problem. Here is the whole assignment, but the main problem I am having is displaying the average grade in the JTextPane:
Write a Swing program that declares an empty array of grades with a maximum length 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 count 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 is below. I don't know if there is some typo I am missing, but I have looked through it many times already and haven't found anything. Any help would be appreciated.

import java.awt.*;
 import java.awt.event.*;
 import javax.swing.*;
 import javax.swing.text.*;
 import java.text.DecimalFormat;
 // Begin public class AveragingGrades extends JFrame implements ActionListener
 public class AveragingGrades extends JFrame implements ActionListener

 boolean firstRun = true;
 int arrayLength;
 double[] numbers = new double[50];

 JTextPane displayPane = new JTextPane();
 DecimalFormat format = new DecimalFormat("######.##");

 public AveragingGrades()
 super("Averaging Grades");

 //Begin public Container createContentPane()
 public Container createContentPane()
 JPanel mainPanel = new JPanel();
 mainPanel.setLayout(new FlowLayout());

 displayPane = addTextToTextPane();
 JScrollPane scrollPane = new JScrollPane(displayPane);
 scrollPane.setPreferredSize(new Dimension(190, 300));
 Container c = getContentPane();
 c.setLayout(new FlowLayout());

 return c;
 } //End public Container createContentPane()

 //Begin public JTextPane addTextToTextPane()
 public JTextPane addTextToTextPane()
 Document doc = displayPane.getDocument();
 //The following lines generate JTextPane attributes on-the-fly
 //to simplify adding text to the JTextPane
 StyleContext style = StyleContext.getDefaultStyleContext();
 AttributeSet set = style.addAttribute(SimpleAttributeSet.EMPTY,StyleConstants.ALIGN_LEFT,style);


 for(int lcv = 0; lcv < arrayLength; lcv++)
 {doc.insertString(doc.getLength(), String.valueOf(numbers[lcv])+ "\n" ,set);}

 System.out.println("Program Started.");
 {double average = average();
 doc.insertString(doc.getLength(),"Average: " + String.valueOf(format.format(average))+ "\n",set);}
 firstRun = false;
 catch(BadLocationException ble)
 {System.err.println("Couldn't insert text.");}

 //End public JTextPane addTextToTextPane()
 return displayPane;

 // Begin public JMenuBar createMenuBar()
 public JMenuBar createMenuBar()
 // Create an instance of the menu
 JMenuBar mnuBar = new JMenuBar();

 //Construct and add the File menu
 JMenu mnuFile = new JMenu("File");

 //Construct and add the menu command to add grades
 JMenuItem mnuFileNewGrades = new JMenuItem("Enter New Grades");

 //Construct and add the Exit Command
 JMenuItem mnuFileExit = new JMenuItem("Exit");

 return mnuBar;
 } // End public JMenuBar createMenuBar()

 // Begin public void actionPerformed(ActionEvent e)
 public void actionPerformed(ActionEvent e)
 String arg = e.getActionCommand();

 if(arg == "NewGrades")
 //Resets the array so no values get accidentally figured in
 for(int lcv = 0 ; lcv < numbers.length; lcv++)
 numbers[lcv] = 0.0;

 double grade = 0;
 int counter = 0;

 //Continue inputting grades until the user enters the sentinel
 //value -1, or the array is completely filled
 while(grade != -1 && counter < 50)
 grade = Double.parseDouble(JOptionPane.showInputDialog(null, "Enter Grade", "Enter Value",JOptionPane.INFORMATION_MESSAGE));numbers[counter] = grade;
 if(grade != -1)

 catch(NumberFormatException nfe) //User enters characters or nothing at all
 System.out.println("Number Format Exception");
 JOptionPane.showMessageDialog(null, "You have entered an invalid number. If you are " + "finished, enter -1.", "Error",
 catch(NullPointerException npe) //User clicks "Cancel" instead of entering -1
 System.out.println("Null Pointer Exception");
 JOptionPane.showMessageDialog(null, "If you are finished, enter -1.", "Error",JOptionPane.INFORMATION_MESSAGE);


 arrayLength = counter;

 if(arg == "Exit")
 System.out.println("Program Terminated.");

 } // End public void actionPerformed(ActionEvent e)

 //Begin public double average()
 public double average()
 double sum = 0 , average;
 int totalPositiveCount = 0;

 for(int lcv = 0 ; lcv < arrayLength; lcv++)
 if(numbers[lcv] > 0)
 sum += numbers[lcv];
 average = sum / (double) totalPositiveCount;

 return average;
 } //End public double average()

 //Sorts the array using a bubble sort method
 //Begin public void sort()
 public void sort()
 boolean change;

 change = false;

 for (int lcv = 0 ; lcv < arrayLength - 1 ; lcv++)
 if(numbers[lcv] > numbers[lcv + 1])
 change = true;
 } //End public void sort

 //Swaps two elements in the array
 //Begin public void swap(int index)
 public void swap(int index)
 double temp = numbers[index];
 numbers[index] = numbers[index + 1];
 numbers[index + 1] = temp;
 } //End public void swap(int index)

 //Begin public static void main(String[] args)
 public static void main(String[] args)
 AveragingGrades m = new AveragingGrades();
 m.setJMenuBar( m.createMenuBar());
 m.setContentPane( m.createContentPane());
 m.setSize(200, 375);
 JOptionPane.showMessageDialog(null, "To enter grades, select File >> Enter New Grades.\n" +
 "To end input, enter -1 in the grade value box.",
 "Welcome", JOptionPane .INFORMATION_MESSAGE);
  } //End public static void main(String[] args)

 } // End public class AveragingGrades extends JFrame implements ActionListener

Edited 6 Years Ago by uptheirons1: n/a

First of all always use {} brackets. Look what you wrote:

if(arg == "Exit")
 System.out.println("Program Terminated.");

And what I wrote:

if(arg == "Exit") {
 System.out.println("Program Terminated.");

In your first code, if the if statement is true it will execute only the System.out.println("Program Terminated.") and the System.exit(0) will always be executed, because you didn't put ant brackets.

And I would suggest not to use this:

String arg = e.getActionCommand();

 if(arg == "NewGrades") {


But this:

public void actionPerformed(ActionEvent e)
   Object src = e.getSource();

   if (mnuFileNewGrades == src) {
      // ....
  } else if (mnuFileExit == src) {
     System.out.println("Program Terminated.");

Thanks for the reply. I am going to change my code now to make it look like what you wrote.

EDIT: I just tried the code and it worked. Thanks a lot javaAddict.

Edited 6 Years Ago by uptheirons1: n/a

This question has already been answered. Start a new discussion instead.