How do I avoid identifier expected problem in my Choice component program.

I am somewhat new to Java and operating in 1.5. I am not sure why I am receiving my errors when compiling. I am sure it is something stupid, but I have been working on other programs for 2 days straight now and this is the first problem I have not been able to resolve. My mind is weary.

I have attached my errors from compiling and my actual code as presented. I know I do not use the JOptionPane, I simply had copy and pasted and hadn't gotten around to removing it.

Any help would be greatly appreciated since I've been lost for hours.
C:\Documents and Settings\Administrator\My Documents\Java Student files\Java Data Files Chp 5-8\Chapter05\Choice.java:16: <identifier> expected
		size.add("500%");
                        ^
C:\Documents and Settings\Administrator\My Documents\Java Student files\Java Data Files Chp 5-8\Chapter05\Choice.java:17: <identifier> expected
		size.add("200%");
                        ^
C:\Documents and Settings\Administrator\My Documents\Java Student files\Java Data Files Chp 5-8\Chapter05\Choice.java:18: <identifier> expected
		size.add("150%");
                        ^
C:\Documents and Settings\Administrator\My Documents\Java Student files\Java Data Files Chp 5-8\Chapter05\Choice.java:19: <identifier> expected
		size.add("100%");
                        ^
C:\Documents and Settings\Administrator\My Documents\Java Student files\Java Data Files Chp 5-8\Chapter05\Choice.java:20: <identifier> expected
		size.add("75%");
                        ^
C:\Documents and Settings\Administrator\My Documents\Java Student files\Java Data Files Chp 5-8\Chapter05\Choice.java:21: <identifier> expected
		size.add("50%");
                        ^
C:\Documents and Settings\Administrator\My Documents\Java Student files\Java Data Files Chp 5-8\Chapter05\Choice.java:22: <identifier> expected
		size.add("25%");
                        ^
C:\Documents and Settings\Administrator\My Documents\Java Student files\Java Data Files Chp 5-8\Chapter05\Choice.java:23: <identifier> expected
		size.add("10%");
                        ^
C:\Documents and Settings\Administrator\My Documents\Java Student files\Java Data Files Chp 5-8\Chapter05\Choice.java:24: <identifier> expected
		size.add("Page Width");
                        ^
C:\Documents and Settings\Administrator\My Documents\Java Student files\Java Data Files Chp 5-8\Chapter05\Choice.java:25: <identifier> expected
		size.add("Text Width");
                        ^
C:\Documents and Settings\Administrator\My Documents\Java Student files\Java Data Files Chp 5-8\Chapter05\Choice.java:26: <identifier> expected
		size.add("Whole Page");
                        ^
C:\Documents and Settings\Administrator\My Documents\Java Student files\Java Data Files Chp 5-8\Chapter05\Choice.java:27: <identifier> expected
		size.add("Two Pages");
                        ^

Open in new window

Beyond_LostAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Beyond_LostAuthor Commented:
I'm not sure why but it only seems to have attached the errors file, I will post the code in an additional response.
/*
Programmer:		Progammer Name
Date:			March 27th, 2010
Program Name: 	Choice.java
Purpose:		To demonstrate a choice component
*/

import javax.swing.JOptionPane;
import java.awt.*;
import java.awt.event.*;

public class Choice extends Frame
{
	Panel inputPanel = new Panel();
		Choice size = new Choice();
		size.add("500%");
		size.add("200%");
		size.add("150%");
		size.add("100%");
		size.add("75%");
		size.add("50%");
		size.add("25%");
		size.add("10%");
		size.add("Page Width");
		size.add("Text Width");
		size.add("Whole Page");
		size.add("Two Pages");


	public Choice()
	{
		this.setLayout(new BorderLayout());
			inputPanel.setLayout(new FlowLayout());

		//add components to input panel
		inputPanel.add(size);
			for(int i = 8; i<=20; i++)


		//add panels to frame
		add(inputPanel, BorderLayout.CENTER);

		//overriding the windowClosing() method will allow the user to click the Close button
		addWindowListener(
			new WindowAdapter()
				{
					public void windowClosing(WindowEvent e)
					{
						System.exit(0);
					}
				}
		);
	} //end of constructor method

		public static void main(String[] args)
		{
			Choice f = new Choice();
			f.setBounds(200,200,1100,300);
			f.setTitle("Choice Demonstration");
			f.setVisible(true);
		} //end of main
}

Open in new window

0
mahomeCommented:
The problem is you can't execute code in the class body, like you do it with all   size.add("...");. Either you put that code in the main method or you put it in a static block.

0
ozlevanonCommented:
You're writing code where only member and method definitions are allowed.
The code starting from line 27 is not a member definition, but actual logic, and must appear inside a method (or a static block)
I suggest you either move this code to the constructor, or, more nicely, write an init() method containing this code, and call if from the constructor
0
Cloud Class® Course: C++ 11 Fundamentals

This course will introduce you to C++ 11 and teach you about syntax fundamentals.

Beyond_LostAuthor Commented:
Okay, Ozelevanon, I know I cannot declare a method within a method, hence the calling it from the constructor...but could you post a quick jotted partial code showing how or where I'd go about doing that? I wrote this code for the init() if that's right...
public void init()
		{
		     Panel inputPanel = new Panel();
		     Choice size = new Choice();
		     size.add("500%");
		     size.add("200%");
		     size.add("150%");
		     size.add("100%");
		     size.add("75%");
		     size.add("50%");
		     size.add("25%");
		     size.add("10%");
		     size.add("Page Width");
		     size.add("Text Width");
		     size.add("Whole Page");
		     size.add("Two Pages");
	     }

Open in new window

0
mahomeCommented:
Here's a full example:
- leave inputPanel and size out of the method, so that they stay members, otherwise tehy woiuld be normal variables
- I called the init() in the constructor, but you can also make it public and call it from main: f.init()

/*
Programmer:             Progammer Name
Date:                   March 27th, 2010
Program Name:   Choice.java
Purpose:                To demonstrate a choice component
*/

import javax.swing.JOptionPane;
import java.awt.*;
import java.awt.event.*;

public class Choice extends Frame
{
        Panel inputPanel = new Panel();
        Choice size = new Choice();              

        public Choice()
        {
                init();
                this.setLayout(new BorderLayout());
                        inputPanel.setLayout(new FlowLayout());

                //add components to input panel
                inputPanel.add(size);
                        for(int i = 8; i<=20; i++)


                //add panels to frame
                add(inputPanel, BorderLayout.CENTER);

                //overriding the windowClosing() method will allow the user to click the Close button
                addWindowListener(
                        new WindowAdapter()
                                {
                                        public void windowClosing(WindowEvent e)
                                        {
                                                System.exit(0);
                                        }
                                }
                );
        } //end of constructor method
        
        protected void init() {
                size.add("500%");
                size.add("200%");
                size.add("150%");
                size.add("100%");
                size.add("75%");
                size.add("50%");
                size.add("25%");
                size.add("10%");
                size.add("Page Width");
                size.add("Text Width");
                size.add("Whole Page");
                size.add("Two Pages");

        }

                public static void main(String[] args)
                {
                        Choice f = new Choice();
                        f.setBounds(200,200,1100,300);
                        f.setTitle("Choice Demonstration");
                        f.setVisible(true);
                } //end of main
}

Open in new window

0
Beyond_LostAuthor Commented:
mahome, I used that exact code and got this error list
C:\Documents and Settings\Administrator\My Documents\Java Student files\Java Data Files Chp 5-8\Chapter05\Choice.java:44: cannot find symbol
symbol  : method add(java.lang.String)
location: class Choice
                size.add("500%");
                    ^
C:\Documents and Settings\Administrator\My Documents\Java Student files\Java Data Files Chp 5-8\Chapter05\Choice.java:45: cannot find symbol
symbol  : method add(java.lang.String)
location: class Choice
                size.add("200%");
                    ^
C:\Documents and Settings\Administrator\My Documents\Java Student files\Java Data Files Chp 5-8\Chapter05\Choice.java:46: cannot find symbol
symbol  : method add(java.lang.String)
location: class Choice
                size.add("150%");
                    ^
C:\Documents and Settings\Administrator\My Documents\Java Student files\Java Data Files Chp 5-8\Chapter05\Choice.java:47: cannot find symbol
symbol  : method add(java.lang.String)
location: class Choice
                size.add("100%");
                    ^
C:\Documents and Settings\Administrator\My Documents\Java Student files\Java Data Files Chp 5-8\Chapter05\Choice.java:48: cannot find symbol
symbol  : method add(java.lang.String)
location: class Choice
                size.add("75%");
                    ^
C:\Documents and Settings\Administrator\My Documents\Java Student files\Java Data Files Chp 5-8\Chapter05\Choice.java:49: cannot find symbol
symbol  : method add(java.lang.String)
location: class Choice
                size.add("50%");
                    ^
C:\Documents and Settings\Administrator\My Documents\Java Student files\Java Data Files Chp 5-8\Chapter05\Choice.java:50: cannot find symbol
symbol  : method add(java.lang.String)
location: class Choice
                size.add("25%");
                    ^
C:\Documents and Settings\Administrator\My Documents\Java Student files\Java Data Files Chp 5-8\Chapter05\Choice.java:51: cannot find symbol
symbol  : method add(java.lang.String)
location: class Choice
                size.add("10%");
                    ^
C:\Documents and Settings\Administrator\My Documents\Java Student files\Java Data Files Chp 5-8\Chapter05\Choice.java:52: cannot find symbol
symbol  : method add(java.lang.String)
location: class Choice
                size.add("Page Width");
                    ^
C:\Documents and Settings\Administrator\My Documents\Java Student files\Java Data Files Chp 5-8\Chapter05\Choice.java:53: cannot find symbol
symbol  : method add(java.lang.String)
location: class Choice
                size.add("Text Width");
                    ^
C:\Documents and Settings\Administrator\My Documents\Java Student files\Java Data Files Chp 5-8\Chapter05\Choice.java:54: cannot find symbol
symbol  : method add(java.lang.String)
location: class Choice
                size.add("Whole Page");
                    ^
C:\Documents and Settings\Administrator\My Documents\Java Student files\Java Data Files Chp 5-8\Chapter05\Choice.java:55: cannot find symbol
symbol  : method add(java.lang.String)
location: class Choice
                size.add("Two Pages");
                    ^
12 errors

Tool completed with exit code 1

Open in new window

0
mahomeCommented:
I didn't check that add-method itself. Where's that add-method coming from. I thought it would be defined in one of the base-classes. What do you want to do? size has to be a list.
0
mahomeCommented:
>>size has to be a list.
Sorry that was wrong.
0
Beyond_LostAuthor Commented:
I would assume the add method would come from java.awt.event.*;
0
Beyond_LostAuthor Commented:
The only thing I need this application to perform, is to display a drop down box with different choices. The Choices do not even have to do anything. They will just exist.
0
VenabiliCommented:
I would assume the add method would come from java.awt.event.*;

Not if you are trying to add elements to a list...

Choice size = new Choice();   is wrong - you do not really want to make a new Frame. What you need is a List to hold your choices. So you need to either use a GUI component list or a Util list (and then to build the GUI one from it).

Look also at your for(int i = 8; i<=20; i++) cycle - what are you trying to do exactly?

Do you want to add a new PANEL for each choice? Or do you want to add a new element in a Combobox?
0
Beyond_LostAuthor Commented:
Yea, I took notice to the For(int.... are and completely removed it. That was from an old piece of code performing a list of numbers 8-20. It worked there, so I was using it as reference but forgot to remove that part. My code that I used a choice in before that worked is this.


/*
Chapter 5:	Reserve a Party Room
Programmer:	Derek Bade
Date:		March 13th, 2010
Filename:	Reservations.java
Purpose:	This program creates a windowed application to reserve a party room.
			It calls an external class named Rooms.
*/

import javax.swing.JOptionPane;
import java.awt.*;
import java.awt.event.*;

public class Reservations extends Frame implements ActionListener
{
	Color lightRed = new Color(255, 90, 90);
	Color lightGreen = new Color(140, 215, 40);

	Rooms room = new Rooms(5,3);

	Panel roomPanel = new Panel();
		TextArea roomDisplay[] = new TextArea[9];

	Panel buttonPanel = new Panel();
		Button bookButton = new Button("Book Room");

	Panel inputPanel = new Panel();
		Label custNameLabel = new Label("Name:");
		TextField nameField = new TextField(15);
		Label custPhoneLabel = new Label("Phone number:");
		TextField phoneField = new TextField(15);
		Label numLabel = new Label("Number in party:");
		Choice numberOfGuests = new Choice();
		CheckboxGroup options = new CheckboxGroup();
			Checkbox nonSmoking = new Checkbox("Nonsmoking", false, options);
			Checkbox smoking = new Checkbox("Smoking", false, options);
			Checkbox hidden = new Checkbox("", true, options);

	public Reservations()
	{
		//set Layouts for frame and three panels
		this.setLayout(new BorderLayout());
			roomPanel.setLayout(new GridLayout(2,4,10,10));
			buttonPanel.setLayout(new FlowLayout());
			inputPanel.setLayout(new FlowLayout());

		//add components to room panel
		for (int i=1; i<9; i++)
		{
			roomDisplay[i] = new TextArea(null,3,5,3);
			if (i<6)
				roomDisplay[i].setText("Room " + i + " Nonsmoking");
			else
				roomDisplay[i].setText("Room " + i + " Smoking");
			roomDisplay[i].setEditable(false);
			roomDisplay[i].setBackground(lightGreen);
			roomPanel.add(roomDisplay[i]);
		}

		//add components to button panel
		buttonPanel.add(bookButton);

		//add components to input panel
		inputPanel.add(custNameLabel);
		inputPanel.add(nameField);
		inputPanel.add(custPhoneLabel);
		inputPanel.add(phoneField);
		inputPanel.add(numLabel);
		inputPanel.add(numberOfGuests);
			for(int i = 8; i<=20; i++)
				numberOfGuests.add(String.valueOf(i));
		inputPanel.add(nonSmoking);
		inputPanel.add(smoking);

		//add panels to frame
		add(buttonPanel, BorderLayout.SOUTH);
		add(inputPanel, BorderLayout.CENTER);
		add(roomPanel, BorderLayout.NORTH);

		//assign ActionListener to each Button
		bookButton.addActionListener(this);

		//overriding the windowClosing() method will allow the user to click the Close button
		addWindowListener(
			new WindowAdapter()
				{
					public void windowClosing(WindowEvent e)
					{
						System.exit(0);
					}
				}
		);
	} //end of constructor method

		public static void main(String[] args)
		{
			Reservations f = new Reservations();
			f.setBounds(200,200,1100,300);
			f.setTitle("Reserve a Party Room");
			f.setVisible(true);
		} //end of main

		public void actionPerformed(ActionEvent e)
		{
			//test for button clicks
			String arg = e.getActionCommand();
			if (arg == "Book Room")
				if (hidden.getState())
			{
				JOptionPane.showMessageDialog(null, "You must select Nonsmoking or Smoking.", "Error", JOptionPane.ERROR_MESSAGE);
			}
			else
			{
				int available = room.bookRoom(smoking.getState());

				if (available > 0) //room is available
				{
					roomDisplay[available].setBackground(lightRed); //display room as occupied
					roomDisplay[available].setText(
													roomDisplay[available].getText() +
													"\n"+
													nameField.getText() +
													" " +
													phoneField.getText() +
													"\nparty of " +
													numberOfGuests.getSelectedItem()
												); //display info in room
					clearFields();
				}
				else //room is not available
				{
					if (smoking.getState())
						JOptionPane.showMessageDialog(null, "Smoking is full.","Error", JOptionPane.INFORMATION_MESSAGE);
					else
						JOptionPane.showMessageDialog(null, "Nonsmoking is full.", "Error", JOptionPane.INFORMATION_MESSAGE);
					hidden.setState(true);
				}//end of else block that checks the available room number
			}//end of else block that checks the state of the hidden option button
	// Clear Buttons not yet functional, considering incorporating button arrays next chapter
			if (arg == "Clear Room One")
			{
				roomDisplay[0].setBackground(lightGreen);
				roomDisplay[0].setText("Room " + "<I'm lost>" + " Nonsmoking");
			}

		}//end of actionPerformed() method

	//reset the text fields and choice component
	void clearFields()
	{
		nameField.setText("");
		phoneField.setText("");
		numberOfGuests.select(0);
		nameField.requestFocus();
		hidden.setState(true);
	} //end of clearFields() method

} //end of Reservations class

Open in new window

0
VenabiliCommented:
K - now let's get back to the current code.

>The only thing I need this application to perform, is to display a drop down box with different choices.
The your size should be of the type javax.swing.JList or java.awt.List
Looking at what your other code is doing, I suspect you want to latter.

Can you try to use it in your code and then  post the code if you still cannot make it work? :)

PS: I won't do the code for you (for more than one reason) but I will work with you so you can get the code done.
0
Beyond_LostAuthor Commented:
Also, Ignore anything regarding the clear buttons section remaining in that code, I had deleted it all to take care of higher priority problems since that was just me being curious. I see I missed a few pieces of the code
0
VenabiliCommented:
Ok - look at mt latest comment, try to implement it and post back the current CLEANED code. :) Let's get this built properly.
0
Beyond_LostAuthor Commented:
Yes, well I Import java.awt.* and java.awt.event.* to import them and all subclasses. I'm not sure what you're asking me to try and do that I haven't already.
0
VenabiliCommented:
Change the type of your size variable.

You need it to hold the combobox. The class Choice is the full Frame, not the combobox.

After this - please post your code.
0
Beyond_LostAuthor Commented:
Sigh, so... My size can not be Choice, however; I still need a Choice to create the drop down frame. I need to declare size as String to populate the Frame?
0
VenabiliCommented:
You have public class Choice extends Frame
which means that Choice is your full frame - which you initialize inside of your void main:

public static void main(String[] args)
            {
                  Choice f = new Choice(); //here is where the Choice is created.
                  f.setBounds(200,200,1100,300);
                  f.setTitle("Choice Demonstration");
                  f.setVisible(true);
            } //end of main


You do not need size to be of Choice type either-  but you need a List element that will show you the choices inside of the panel that stays inside of the Choice Frame.

Think of this like shoe boxes (or any other type of boxes):
Choice is your external box. Inside of it you put a Panel (the layer of paper on which you can put things so you do not get the box all dirty). And inside the Panel you need to somehow add the combobox. This combobox is your size variable. So do you need a Choice (another box) or do you need a List inside of the box (the thing that has the options on it)?
0
Beyond_LostAuthor Commented:
Okay, well I'm gonna go and undo everything I just changed....because I'm pretty sure I went even further off track with this
/*
Programmer:		Derek Bade
Date:			March 27th, 2010
Program Name: 	Choice.java
Purpose:		To demonstrate a choice component
*/

import java.awt.*;
import java.awt.event.*;

public class Choice extends Frame
{
	String size;

	Panel inputPanel = new Panel();
	Choice sizeFrame = new Choice();
		String[] size = new String["500%"];
		size[1] = ["200%"];
		size[2] = ["150%"];
		size[3] = [("100%"];
		size[4] = ["75%"];
		size[5] = ["50%"];
		size[6] = ["25%"];
		size[7] = ["10%"];
		size[8] = ["Page Width"];
		size[9] = ["Text Width"];
		size[10] = ["Whole Page"];
		size[11] = ["Two Pages"];


	public Choice()
	{
		this.setLayout(new BorderLayout());
			inputPanel.setLayout(new FlowLayout());

		//add components to input panel
		inputPanel.add(size);
			for(int i = 8; i<=20; i++)


		//add panels to frame
		add(inputPanel, BorderLayout.CENTER);

		//overriding the windowClosing() method will allow the user to click the Close button
		addWindowListener(
			new WindowAdapter()
				{
					public void windowClosing(WindowEvent e)
					{
						System.exit(0);
					}
				}
		);
	} //end of constructor method

		public static void main(String[] args)
		{
			Choice f = new Choice();
			f.setBounds(200,200,1100,300);
			f.setTitle("Choice Demonstration");
			f.setVisible(true);
		} //end of main
}

Open in new window

0
Beyond_LostAuthor Commented:
And what I need is a simple drop down box. Um, an example would be in Microsoft word, You use one to select your Font Size, or Type. I thought Choice was the right way to go with that, but as you know, I'm doing it wrong.

okay so my main box is named Choice
then I need a Panel, which I guess could named ChoicePanel. Then I need my Combobox, or the contents, which I could name size. If i'm getting what you're throwing out there. Now I just have to figure out how I'm going to set this all up
0
Beyond_LostAuthor Commented:
Is it necessary for me to create an additional java file, like I did with the reservations application, in order to call it into my main java file? Or can I do it without calling an added file in? because I've yet to do an application using a choice with only one Java file.
0
VenabiliCommented:
One file will do. :) And you got it correctly :)

OK - I start line by line:

1. Choice sizeFrame = new Choice();

Again - you do NOT need this here - see my comment above why. Just delete it.

2. String size;
Remove this - you redefine size as String[] a bit lower so no need to have it.

3. String[] size = new String["500%"];
That's a wrong syntax...
Instead do as I already mentioned:
List size= new List();
size.add("500%");
and so one :)

4. for(int i = 8; i<=20; i++)
Remove this.

Now do these changes and try to compile :) Post back with the latest code if you still cannot make it work.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Beyond_LostAuthor Commented:
Thanks, I actually figured it out via another means. I converted it into an applet and it was cake. I found out my original version (which never made it here) was probably accurate, because my compiler hates me apparently. I told it to compile, and it refused to work. I copied all on my code, deleted the file, and recreated it, and it  compiled and executed perfectly. So all my attempted "corrections" before I came here were actually just leading me away. TextPad compiler just decided that it was not going to function correctly. Thanks for all the help tho.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.