Solved

Vector addElement -->Identifier Expecte error???

Posted on 2000-04-13
6
548 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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
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

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Fibonacci challenge 11 84
@SBGen Method 3 25
what is the difference between "sudo su" and "su - root" 6 75
Java and GPO 11 47
For customizing the look of your lightweight component and making it look opaque like it was made of plastic.  This tip assumes your component to be of rectangular shape and completely opaque.   (CODE)
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 basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.

757 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