?
Solved

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

Posted on 2010-03-29
24
Medium Priority
?
477 Views
Last Modified: 2013-11-23
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

0
Comment
Question by:Beyond_Lost
  • 13
  • 6
  • 4
  • +1
24 Comments
 

Author Comment

by:Beyond_Lost
ID: 28983181
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
 
LVL 10

Expert Comment

by:mahome
ID: 28986302
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
 
LVL 8

Expert Comment

by:ozlevanon
ID: 28986405
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
Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

 

Author Comment

by:Beyond_Lost
ID: 28988320
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
 
LVL 10

Expert Comment

by:mahome
ID: 28989911
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
 

Author Comment

by:Beyond_Lost
ID: 28990711
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
 
LVL 10

Expert Comment

by:mahome
ID: 28992286
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
 
LVL 10

Expert Comment

by:mahome
ID: 28992405
>>size has to be a list.
Sorry that was wrong.
0
 

Author Comment

by:Beyond_Lost
ID: 28992762
I would assume the add method would come from java.awt.event.*;
0
 

Author Comment

by:Beyond_Lost
ID: 28995147
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
 
LVL 20

Expert Comment

by:Venabili
ID: 29000982
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
 

Author Comment

by:Beyond_Lost
ID: 29001673
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
 
LVL 20

Expert Comment

by:Venabili
ID: 29002040
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
 

Author Comment

by:Beyond_Lost
ID: 29002059
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
 
LVL 20

Expert Comment

by:Venabili
ID: 29002113
Ok - look at mt latest comment, try to implement it and post back the current CLEANED code. :) Let's get this built properly.
0
 

Author Comment

by:Beyond_Lost
ID: 29002169
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
 
LVL 20

Expert Comment

by:Venabili
ID: 29002254
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
 

Author Comment

by:Beyond_Lost
ID: 29003269
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
 
LVL 20

Expert Comment

by:Venabili
ID: 29003678
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
 

Author Comment

by:Beyond_Lost
ID: 29004308
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
 

Author Comment

by:Beyond_Lost
ID: 29004834
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
 

Author Comment

by:Beyond_Lost
ID: 29005134
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
 
LVL 20

Accepted Solution

by:
Venabili earned 1000 total points
ID: 29006344
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
 

Author Comment

by:Beyond_Lost
ID: 29033570
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

Featured Post

The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.
Suggested Courses
Course of the Month4 days, 16 hours left to enroll

601 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question