Link to home
Start Free TrialLog in
Avatar of Beyond_Lost
Beyond_Lost

asked on

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

Avatar of Beyond_Lost
Beyond_Lost

ASKER

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

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.

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
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

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

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

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.
>>size has to be a list.
Sorry that was wrong.
I would assume the add method would come from java.awt.event.*;
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.
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?
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

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.
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
Ok - look at mt latest comment, try to implement it and post back the current CLEANED code. :) Let's get this built properly.
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.
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.
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?
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)?
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

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
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.
ASKER CERTIFIED SOLUTION
Avatar of Venabili
Venabili
Flag of Bulgaria image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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.