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("######.##");
//Constructor
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.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
scrollPane.setWheelScrollingEnabled(true);
scrollPane.setPreferredSize(new Dimension(190, 300));
mainPanel.add(scrollPane);
Container c = getContentPane();
c.setLayout(new FlowLayout());
c.add(mainPanel);
return c;
} //End public Container createContentPane()
//Begin public JTextPane addTextToTextPane()
public JTextPane addTextToTextPane()
{
Document doc = displayPane.getDocument();
try
{
//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);
displayPane.setParagraphAttributes(set,false);
doc.remove(0,doc.getLength());
for(int lcv = 0; lcv < arrayLength; lcv++)
{doc.insertString(doc.getLength(), String.valueOf(numbers[lcv])+ "\n" ,set);}
if(firstRun)
System.out.println("Program Started.");
else
{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();
setJMenuBar(mnuBar);
//Construct and add the File menu
JMenu mnuFile = new JMenu("File");
mnuFile.setMnemonic(KeyEvent.VK_F);
mnuFile.setDisplayedMnemonicIndex(0);
mnuBar.add(mnuFile);
//Construct and add the menu command to add grades
JMenuItem mnuFileNewGrades = new JMenuItem("Enter New Grades");
mnuFileNewGrades.setMnemonic(KeyEvent.VK_W);
mnuFileNewGrades.setDisplayedMnemonicIndex(0);
mnuFile.add(mnuFileNewGrades);
mnuFileNewGrades.setActionCommand("NewGrades");
mnuFileNewGrades.addActionListener(this);
//Construct and add the Exit Command
JMenuItem mnuFileExit = new JMenuItem("Exit");
mnuFileExit.setMnemonic(KeyEvent.VK_X);
mnuFileExit.setDisplayedMnemonicIndex(1);
mnuFile.add(mnuFileExit);
mnuFileExit.setActionCommand("Exit");
mnuFileExit.addActionListener(this);
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)
{
try
{
grade = Double.parseDouble(JOptionPane.showInputDialog(null, "Enter Grade", "Enter Value",JOptionPane.INFORMATION_MESSAGE));numbers[counter] = grade;
if(grade != -1)
counter++;
}
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",
JOptionPane.INFORMATION_MESSAGE);
}
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;
sort();
addTextToTextPane();
}
if(arg == "Exit")
System.out.println("Program Terminated.");
System.exit(0);
} // 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];
totalPositiveCount++;
}
}
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;
do
{
change = false;
for (int lcv = 0 ; lcv < arrayLength - 1 ; lcv++)
{
if(numbers[lcv] > numbers[lcv + 1])
{
swap(lcv);
change = true;
}
}
}while(change);
} //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.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
m.setJMenuBar( m.createMenuBar());
m.setContentPane( m.createContentPane());
m.setSize(200, 375);
m.setVisible(true);
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