Solved

Vector addElement -->Identifier Expecte error???

Posted on 2000-04-13
6
558 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
Industry Leaders: 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!

 
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

Technology Partners: 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

Suggested Solutions

Title # Comments Views Activity
ejb example issues 3 47
How do I remove an object from a 3 51
junit example issue 2 22
netbeans can not run self signed app 8 7
An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
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…
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…
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…

713 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