Solved

Vector addElement -->Identifier Expecte error???

Posted on 2000-04-13
6
549 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
  • 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 35 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
The viewer will learn how to implement Singleton Design Pattern in Java.

914 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now