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);
        }
        }
        }

take a closer look at your main method, I've pointed out the things to correct below:

//main method executes at run time
		public static void main(String args[])
		{
				//method to add new text to the JTextPane
						{ // REMOVE THIS BRACKET
							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;

// ADD A CLOSING BRACKET HERE => }
								//method to sort arrays
								public void sort(int grades[])
								{
//...

take a closer look at your main method, I've pointed out the things to correct below:

Thank you, but its still giving me that error, with all of these now...

I tried that once, but thought I miscalculated becuase I was getting all these errors now
:\Users\Liz\Desktop\Java\Chapter07\Grades.java:102: <identifier> expected
JFrame.setDefaultLookAndFeelDecorated(true);
^
C:\Users\Liz\Desktop\Java\Chapter07\Grades.java:104: <identifier> expected
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
^
C:\Users\Liz\Desktop\Java\Chapter07\Grades.java:105: <identifier> expected
f.setContentPane(f.createContentPane());
^
C:\Users\Liz\Desktop\Java\Chapter07\Grades.java:106: <identifier> expected
f.setSize(600,375);
^
C:\Users\Liz\Desktop\Java\Chapter07\Grades.java:107: <identifier> expected
f.setVisible(true);
^
C:\Users\Liz\Desktop\Java\Chapter07\Grades.java:109: 'class' or 'interface' expected
}
^
C:\Users\Liz\Desktop\Java\Chapter07\Grades.java:110: 'class' or 'interface' expected
}
^
7 errors

Tool completed with exit code 1


I get that error twice, once with the arrow pointing at the } and once with it pointing right after...this is what I was confused about when I added that before...
Liz

it looks like you'll need to recheck the brackets you used. the last error(s) indicate that those brackets are outside the scope of the class.
I might check in a while whether I can find the other problems, but haven't got a decent editor on this pc that 'll format the code the way it should be formatted, which makes it quite difficult to read

I am sorry, is there a way I can add it that does format it correctly?

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
                public JTextPane addTextToTextPane()
                {
                            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);
        }
}

The only error I am getting now is illegal start on the line 67 illegal start of expression, where the public jtextpane addtexttopane() is...

Any ideas anyone?

You see the code button at the top of the reply box? Click it, then insert your code in the middle of it.

Hey,
You know, you should really work at debugging your own code. It helps you to understand what you're doing incorrectly. And from how it looks to me, you might be the only person who actually understands what it is that you're trying to get done. Point and caseā€¦

From what I understand of java, methods are defined within classes and not inside other methods. You define two new methods inside your main method, and I don't think that's allowed. I might be wrong though so you should check some sort of documentation on the topic. By the way, just because the compiler only detects one error as the code is, doesn't mean that there aren't any more. There may be tons others that will show themselves as soon as you think you've fixed another.

So I suggest that you go through your own code and make sure that everything makes sense logically, it seems that's where your problems are.

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);
}
}
}

I see you have a return statement in your main method. the main method is void, so it can not contain a return statement.
ps: this is your original code, didn't make the changes I suggested earlier in it.
placing code like this doesn't give you the preferred indentations, but it makes it easier to see which line your error message talks about

/*
	Chapter 7:	Classics on DVD
	Programmer: Elizabeth Wilson
	Date:		March 28, 2010
	Filename:	Grades.java
	Purpose:	This program creates a Swing interface for calculating grade averages.
*/

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();
				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;
		}



		//main method executes at run time
		public static void main(String args[])
		{
								//get grades from instructor
								for (int i=0; i!=-1; i++)
								{
								String newTitle = JOptionPane.showInputDialog(null, "Please enter the student's grade");
							}

			JFrame.setDefaultLookAndFeelDecorated(true);
			grades f = new grades();
			f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
			f.setContentPane(f.createContentPane());
			f.setSize(600,375);
			f.setVisible(true);
		}
		//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();
		}
				///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;
		}
}

Thank you for yoru help stult, I hope you udnerstand that I am not just being lazy, I jsut dont know where else to turn for help in getting this, understanding it....I am taking notes as I go, I have learned from you at least that you can't have a return method in a void statement. The way this is being taught isnt helpful to me (its online, I teach myself) So thank you for yoru time of looking at this instead of just being like some and telling me I need to debug my own code. I have taken visual basic before, and advanced visual basic, but cannot fully understand this yet. At least not this project.

My errors now are
C:\Users\Liz\Desktop\Java\Chapter07\Grades.java:64: cannot find symbol
symbol : class grades
location: class Grades
grades f = new grades();
^
C:\Users\Liz\Desktop\Java\Chapter07\Grades.java:64: cannot find symbol
symbol : class grades
location: class Grades
grades f = new grades();
^
C:\Users\Liz\Desktop\Java\Chapter07\Grades.java:77: int cannot be dereferenced
if (grades[element].compareTo(grades[element+1])>0)
^
C:\Users\Liz\Desktop\Java\Chapter07\Grades.java:79: swap(java.lang.String[],int,int) in Grades cannot be applied to (javax.swing.JLabel,int,int)
swap(title, element, element+1);
^
C:\Users\Liz\Desktop\Java\Chapter07\Grades.java:82: cannot find symbol
symbol : method addTextToTextPane()
location: class Grades
addTextToTextPane();
^
5 errors

Tool completed with exit code 1

I went back and made sure that I made yoru changes, I only saw two...But I also deleted some code that I thought maybe was unnecessary...this is very frustrating to me becuase I can't UNDERSTAND why im getting the error messages im getting.

C:\Users\Liz\Desktop\Java\Chapter07\Grades.java:64: cannot find symbol
symbol : class grades
location: class Grades
grades f = new grades();
^
C:\Users\Liz\Desktop\Java\Chapter07\Grades.java:64: cannot find symbol
symbol : class grades
location: class Grades
grades f = new grades();
^

these errors are caused by the fact that Java is case sensitive. grade does not equal Grade. your compiler is looking for a class grades, but that doesn't exist, you'll have to change those grades into Grades, that should fix those.

^
C:\Users\Liz\Desktop\Java\Chapter07\Grades.java:77: int cannot be dereferenced
if (grades[element].compareTo(grades[element+1])>0)
^

grades[element] returns an int, this is not an Object but a primitive. you can't call a method from a primitive. you could replace this with:
(Integer.valueOf(grades[element]).compareTo(Integer.valueOf(grades[element+1])
but you could also easily
if ( grades[element] > grades[element+1])

C:\Users\Liz\Desktop\Java\Chapter07\Grades.java:79: swap(java.lang.String[],int,int) in Grades cannot be applied to (javax.swing.JLabel,int,int)
swap(title, element, element+1);

this method exists, but it is expecting an array of String Objects as first parameter, not a single String.
I don't really understand what it is you're trying to do with that 'swap' method, but I think you're swapping the wrong data.

C:\Users\Liz\Desktop\Java\Chapter07\Grades.java:82: cannot find symbol
symbol : method addTextToTextPane()
location: class Grades
addTextToTextPane();
^

you're calling a local method that doesn't exist. you'll need to write a new method that's called addTextToTextPane and that takes no parameters, then it 'll work (you'll also have to put in the right code to make it do what you want it to do, off course :) ).

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.