The Artima Developer Community
Sponsored Link

Java Answers Forum
Adding to a Vector problem

3 replies on 1 page. Most recent reply: Jun 5, 2002 10:40 PM by Matt Gerrans

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 3 replies on 1 page
jake

Posts: 83
Nickname: onorok
Registered: May, 2002

Adding to a Vector problem Posted: Jun 5, 2002 6:56 PM
Reply to this message Reply
Advertisement
Why won't this work??
-----------------------------------
import java.util.Vector;
public class Customer
{
public Vector bankCustomers;
String first_name, last_name;
long phone_number;

public Customer()
{
bankCustomers=new Vector();
}

public void newCustomer(String Fname, String Lname, long phoneN)
{
this.first_name=Fname;
this.last_name=Lname;
this.phone_number=phoneN;

//Adds the new Customer to the BankCustomer Vector.
bankCustomers.addElement(newCustomer(Fname,Lname,phoneN));
}
}

--------------------------------------------------
this is the error.


--------------------Configuration: JDK version 1.3 <Default>------------------------------------------------
C:\Program Files\Xinox Software\JCreator\MyProjects\Customer.java:20: 'void' type not allowed here
bankCustomers.addElement(newCustomer(Fname,Lname,phoneN));
^
1 error
Process completed.


Matt Gerrans

Posts: 1153
Nickname: matt
Registered: Feb, 2002

Re: Adding to a Vector problem Posted: Jun 5, 2002 8:58 PM
Reply to this message Reply
There are many problems:
- You didn't format your post using the [java] tag, which makes it hard for us to help you!!! (when you are writing a post, look at the bottom of the page, where it says Formatting Your Post.
- Your subject has nothing to do with your actual question (you problem is with object construction, not adding to a Vector).
- Constructors are declared with no return typr, not even void.
- The constructor name should match the class name exactly (it should be Customer(), not newCustomer()).
- When you define your constructor correctly, you will want to call it with a space between the new operatator and the class (or constructor, as it were) name.
- You will probably not want to be calling "new Customer()" inside the Customer constructor, unless you enjoy it when your computer freezes up. This is a recursive call plus memory leak! A double whammy!

jake

Posts: 83
Nickname: onorok
Registered: May, 2002

Re: Adding to a Vector problem Posted: Jun 5, 2002 9:57 PM
Reply to this message Reply
There is a constructor, called public Customer(), and there is a method called newCustomer(), all I want to do is put that newCustomer into a Vector.

Matt Gerrans

Posts: 1153
Nickname: matt
Registered: Feb, 2002

Re: Adding to a Vector problem Posted: Jun 5, 2002 10:40 PM
Reply to this message Reply
I think this is what you are trying to do. I've changed the method parameter names to try and help clarify the difference between the parameters for the constructor and those for the other method.

Incidentally, it seems like this method would be a static one. However, it might be a better idea to have a Customer class and a separate CustomerCollection class, which can contain a collection of Customer objects. Having a Customer object contain a list of Customer objects seems a little confusing (like, does the object contain itself in the list? Do all Customer objects contain all other Customer objects in their lists?).

import java.util.Vector;
 
public class Customer
{
   private Vector bankCustomers = new Vector();
   String         first_name, 
                  last_name;
   long           phone_number;
   
   public Customer( String firstName, String lastName, long phoneNumber )
   {
      first_name   = firstName;
      last_name    = lastName;
      phone_number = phoneNumber;
   }
   
   public void addNewCustomer(String Fname, String Lname, long phoneN )
   {
      // Adds the new Customer to the BankCustomer Vector.
      bankCustomers.addElement( new Customer( Fname,Lname,phoneN ) );
   }
}

Flat View: This topic has 3 replies on 1 page
Topic: Exception Handling in Threads Previous Topic   Next Topic Topic: help with blackjack game GUI

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use