?
Solved

Vector addElement -->Identifier Expecte error???

Posted on 2000-04-13
6
Medium Priority
?
600 Views
Last Modified: 2011-10-03
I'm sure this is a simple question for most of you but I'm just starting out and I've been struggling with it for awhile.
This is the code:import java.util.Vector;

public class MyDirectory1{
      private Vector fieldDirectory = new Vector();
      private Contact fieldC1 = new Contact("Joey","Shabadoo","jshabadoo@iti.com","123-1234");
      
      fieldDirectory.addElement(fieldC1);
      
} // end class

I'm getting an "identifier expected" error when I try to compile and it points to the ")" on the addElement method.
What does this mean?
0
Comment
Question by:KevB
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
6 Comments
 
LVL 19

Expert Comment

by:Jim Cakalic
ID: 2714511
What you've defined is a class with no methods but with executable code. The declarations of fieldDirectory and Contact are okay because they declare instance variables -- variables which can have different values for each object created of class MyDirectory1. Instance variables can have initializers which give them default values. So far so good. The line

    fieldDirectory.addElement(fieldC1);

is an executable statement. The compiler is telling you, in its own roundabout way, that variable declarations outside of methods are okay but executable statements are not. You need to enclose this statment inside a method or constructor. Assuming that you mean to initialize an object of the class, you'll want a constructor:

    public MyDirectory1() {
        fieldDirectory.addElement(fieldC1);
    }

A constructor is a special method that is called when an object of your class is created to initialize the object variables to a known state. In your case, you want the Vector fieldDirectory to contain a Contact object. This particular constructor takes no arguments which means you can make a MyDirectory1 object by saying:

    MyDirectory1 md1 = new MyDirectory1();

Of course, you will also have to have a Contact class somewhere. To this point, I can't tell anything about the Contact class except that to make an object of the class requires four String arguments. Here is how I declare the class so far.

    public class Contact {
        String f1, f2, f3, f4;
       
        public Contact(String f1, String f2, String f3, String f4) {
            this.f1 = f1;
            this.f2 = f2;
            this.f3 = f3;
            this.f4 = f4;
        }
    }

I'm sure you will pick much better names than I have.

Best regards,
Jim Cakalic
0
 
LVL 1

Accepted Solution

by:
computerpunk earned 140 total points
ID: 2714519
IT is because the fieldDirectory.addElement(fieldC1); is not within a method body. The executable statement should be inside main inside the constructor of the class MyDirectory1.
0
 
LVL 16

Expert Comment

by:heyhey_
ID: 2714915
WHOW

expert answer
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 16

Expert Comment

by:heyhey_
ID: 2715777
hehe
0
 
LVL 19

Expert Comment

by:Jim Cakalic
ID: 2715880
I guess my answer was too long.
0
 
LVL 16

Expert Comment

by:heyhey_
ID: 2715957
the classic EE problem - usually the questioner accepts the posted answer even if it was wrong.
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
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.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
Suggested Courses

718 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