Hi Everyone. I am taking a class on java, and have made it to chapter 7 without many problems. I am having really bad problems with this, getting an illegal start of expression where it says public void sort....I have tried so many things, I probably even have things out of order now, and I am so confused. Been working on this for more than a week. My assignment is to create a program where the JOptionPane opens an input box and asks the instructor to enter grades, and then enters -1 to exit the program (I know I dont have this part in here yet, becuase everytime I go to check it, if I add anything else I get multiple more errors, and so i would like to get this part done before I finish the rest.
At the end of the program when the instructor enters -1 the program is going to go out and average the grades and display, but I know there again i do not have all the code for that. This problem here is just so frustrating, I ahve counted braces, everything, and I really dont understand what I am doing.
Thanks,
Liz
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;
int total = 0;
DecimalFormat twoDigits = new DecimalFormat ("##0.00");
//initialize data in arrays
int[] grades = new int[50];
//create the content pane
public Container createContentPane()
{
//construct and populate the north panel
JPanel northPanel = new JPanel();
northPanel.setLayout(new FlowLayout());
northPanel.add(title);
//create the JTextPane and center panel
JPanel centerPanel = new JPanel();
textPane = addTextToTextPane();
JScrollPane scrollPane = new JScrollPane(textPane);
scrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
scrollPane.setPreferredSize(new Dimension(500, 200));
centerPanel.add(scrollPane);
//create Container and set attributes
Container c = getContentPane();
c.setLayout(new BorderLayout(10,10));
c.add(northPanel,BorderLayout.NORTH);
c.add(centerPanel,BorderLayout.CENTER);
return c;
}
///method to swap two elements of an arry
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;
}
//main method executes at run time
public static void main(String args[])
{
//method to add new text to the JTextPane
{
Document doc = textPane.getDocument();
try
{
//clear previous text
doc.remove(0, doc.getLength());
//get grades from instructor
for (int i=0; i!=-1; i++)
{
String newTitle = JOptionPane.showInputDialog(null, "Please enter the student's grade");
}
}
catch(BadLocationException ble)
{
System.err.println("Couldn't insert text.");
}
return textPane;
//method to sort arrays
public void sort(int grades[])
{
//loop to control number of passes
for (int pass = 1; pass < grades.length; pass++)
{
for (int element = 0; element <grades.length-1; element++)
if (grades[element].compareTo(grades[element+1])>0)
{
swap(title, element, element+1);
}
}
addTextToTextPane();
}
JFrame.setDefaultLookAndFeelDecorated(true);
grades f = new grades();
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setContentPane(f.createContentPane());
f.setSize(600,375);
f.setVisible(true);
}
}
}