Solved

Vector addElement -->Identifier Expecte error???

Posted on 2000-04-13
6
553 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
The Eight Noble Truths of Backup and Recovery

How can IT departments tackle the challenges of a Big Data world? This white paper provides a roadmap to success and helps companies ensure that all their data is safe and secure, no matter if it resides on-premise with physical or virtual machines or in the cloud.

 
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

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

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

Suggested Solutions

After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
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 …
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.

832 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