Quantcast
Channel: Programming Forums
Viewing all articles
Browse latest Browse all 51036

not sorting or entering into calculation method

$
0
0
This program compiles, however, it only lets me insert one grade before the window closes and it doesn't enter the sort or the calculation of the average grade. I am not sure where I am going wrong, any help would be greatly appreciated.

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

public class Average extends JFrame implements ActionListener
{
	JTextPane gradesPane = new JTextPane();
	JButton enterGrades = new JButton("Enter Grades");
	String grades[] = new String[50];
	int SENTINEL = 0;
	DecimalFormat formatter = new DecimalFormat("######.##");

	public Average()
	{
		super("Grade Average");
	}

	//create the menu system
	public JMenuBar createMenuBar()
	{
		//create an instance of the menu
		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);

		JMenuItem mnuFileExit = new JMenuItem("Exit");
			mnuFileExit.setMnemonic(KeyEvent.VK_X);
			mnuFileExit.setDisplayedMnemonicIndex(1);
			mnuFile.add(mnuFileExit);
			mnuFileExit.setActionCommand("Exit");
			mnuFileExit.addActionListener(this);
		
		
			return mnuBar;
	}

	//create the content pane
	public Container createContentPane()
	{
		//construct and populate the north panel
		JPanel northPanel = new JPanel();
		northPanel.setLayout(new FlowLayout());
		northPanel.add(enterGrades);
		enterGrades.addActionListener(this);

		//construct and populate the central panel
		JPanel centralPanel = new JPanel();
		centralPanel.setLayout(new FlowLayout());
		centralPanel.add(gradesPane);
		JScrollPane scrollPane = new JScrollPane(gradesPane);
						scrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
						scrollPane.setPreferredSize(new Dimension(500,200));
		centralPanel.add(scrollPane);
		gradesPane.setEditable(false);

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

		return c;
	}
//event to process user clicks
	public void actionPerformed(ActionEvent e)
	{
		String arg = e.getActionCommand();
		//user clicks the enter by combo box
		if (e.getSource() == enterGrades)
			gradeCollector();
		// user clicks the exit on the file menu
		if (arg == "Exit")
			System.exit(0);
	}

	public void gradeCollector()
	{
		int arrayPop = 0;

		while (SENTINEL != -1)
		{
			String grade = JOptionPane.showInputDialog("Please enter a grade:");
			checkGrade(grade);
			SENTINEL = Integer.parseInt(grade);
			grades[arrayPop] = grade;
			arrayPop++;

			sort(grades);
		}
	}

	public void sort(String tempArray[])
	{
		//loop to control number of passes
		for (int pass = 1; pass < tempArray.length; pass++)
		{
			for (int element = 0; element < tempArray.length - 1; element++)
				if(tempArray[element].compareTo(tempArray[element + 1]) > 0)
				{
					swap(grades, element, element + 1);
				}
		}

		addTextToTextPane();
	}


	public JTextPane addTextToTextPane()
	{
		Document doc = gradesPane.getDocument();
		try
		{
			//clear previous text
			doc.remove(0, doc.getLength());

			//insert detail
			for (int j = 0; j<grades.length; j++)
			{
				doc.insertString(doc.getLength(), grades[j] + "\t",gradesPane.getStyle("Bold"));
			}


		}

		catch(BadLocationException ble)
		{
			System.err.println("Couldn't insert text.");
		}

		return gradesPane;
	}

	public void swap(String swapArray[], int first, int second)
	{
		String hold; //temporary holding area for swap
		hold = swapArray[first];
		swapArray[first] = swapArray[second];
		swapArray[second] = hold;
	}

	public void checkGrade(String value)  //implemented later
	{
		int intValue = 0;

		intValue = Integer.parseInt(value);

		while ((intValue > 100) || (intValue < -1))
		{
			JOptionPane.showMessageDialog(null,"that is not a valid entry.");
			String grade = JOptionPane.showInputDialog("Please enter a grade:");

			intValue = Integer.parseInt(grade);
		}
	}

	public double averager(String genericArray[])
	{
		int total = 0;
		int gradeCount = 0;
		double finalAverage;
		for (int z = 0; z < genericArray.length; z++)
		{
			total += Integer.parseInt(genericArray[z]);
			gradeCount++;

			if (Integer.parseInt(genericArray[z]) <= 0)
			gradeCount --;
		}

		finalAverage = total / gradeCount;

		return finalAverage;
	}

	public String gradeConverter(double decGrade)
	{
		String result = " ";

		if (decGrade > 89)
			result = ("Your final grade is " + formatter.format(decGrade) + " an A.");
		else
			if (decGrade > 79)
				result = ("Your final grade is " + formatter.format(decGrade) + " a B.");
			else
				if (decGrade > 69)
					result = ("Your final grade is " + formatter.format(decGrade) + " a C.");
				else
					if (decGrade > 59)
						result = ("Your final grade is " + formatter.format(decGrade) + " a D.");
					else result = ("Your final grade is " + formatter.format(decGrade) + " an F. Sorry :(/>/>/>/>");

		return result;
	}

	public static void main(String args[])
	{
		Average a = new Average();
		a.setJMenuBar(a.createMenuBar());
		a.setContentPane(a.createContentPane());
		a.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		a.setSize(600,375);
		a.setVisible(true);
	}
}


Viewing all articles
Browse latest Browse all 51036

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>