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

public class Grades extends JFrame
{
	//construct components
	JLabel title = new JLabel("Grades");
	JTextPane textPane = new JTextPane();
	int numberOfGrades = 0;
	double total = 0;
	DecimalFormat twoDigits = new DecimalFormat ("##0.00");

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

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

			JPanel centerPanel = new JPanel();
				centerPanel.setLayout(new FlowLayout());


			//create the JTextPane and center panel
			textPane = addTextToTextPane();
				JScrollPane scrollPane = new JScrollPane(textPane);
					scrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
					scrollPane.setPreferredSize(new Dimension(500, 200));
				centerPanel.add(title);
				centerPanel.add(scrollPane);

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

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

					for (int j=0; j<numberOfGrades; j++)
					{
						doc.insertString(doc.getLength(), "\t" + String.valueOf(grades[j]) + "\n");
			}
		}
		catch(BadLocationException ble)
		{
			System.err.println("Couldn't insert grade.");
	}
	return textPane;
}

		//method to sort arrays
		public void sort(int values[], int count)
		{
			//loop to control number of passes
				for (int pass = 1; pass < count; pass++)
					{
					for (int element = 0; element < count; element++)
					if (values[element] > values [element +1])
						{
									swap(values, element, element+1);
							}
						}
						addTextToTextPane();
		}


		///method to swap two elements of an arry
						public void swap(int swapArray[], int first, int second)
						{
							int hold; //temporary holding area for swap
							hold = swapArray[first];
							swapArray[first] = swapArray[second];
							swapArray[second] = hold;
		}


		//main method executes at run time
		public static void main(String args[])
		{


			JFrame.setDefaultLookAndFeelDecorated(true);
			Grades f = new Grades();
			f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

			int intInput = Integer.parseInt(JOptionPane.showInputDialog(null, "Please enter grade (0-100) or -1 to exit"));
			while (intInput != -1)
			{
				if (intInput > 0)
				{
					grades[numberOfGrades] = intInput;
					total += intInput;
					numberOfGrades++;
				}
				intInput = Integer.parseInt(JOptionPane.showInputDialog(null, "Please enter grade (0-100) or -1 to exit"));
		}
		sort(grades, numberOfGrades);
			f.setContentPane(f.createContentPane());
			f.setSize(600,375);
			f.setVisible(true);
		}


}

Ok I had posted a thread before with a different problem, and so I thought it might be best to start a new thread...

I am getting an error now that on line 60 insertString cannot be applied to that becuase its an int, but I am trying to use the valueOf method to return a string value from its int argument...so I am really confused!
Thanks for anyones help

Edited 6 Years Ago by KSUliz: n/a

Not sure, but I think you need a third parameter in your call to insertString. The Java API says that insertString takes an int, a String and an AttributeSet parameter, so I would check that first.

Not sure, but I think you need a third parameter in your call to insertString. The Java API says that insertString takes an int, a String and an AttributeSet parameter, so I would check that first.

Thought that the attribute set parameter would be optional and only used if I was using styles....

Nope, it's not optional. If it were optional in Java another method would be defined, insertString(int, String).

you have to change the code lik this:
doc.insertString(doc.getLength(), "\t" + String.valueOf(grades[j]) + "\n",null);

but you still have another error because of calling a non static variables (numberOfGrades,total) from static context.

it is possible to have one more error at run time and it could be related to synchronization (but not sure) just try it.

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