6 Replies - 461 Views - Last Post: 06 December 2012 - 11:23 PM Rate Topic: -----

#1 RidleyMoose  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 02-December 12

Linking Arrays and Objects through Constructors

Posted 06 December 2012 - 03:44 PM

Hello!

I am trying to pass an array as a parameter of a constructor in my program. Entry is a class used to modify the objects amy and brian instance variables. Addressbook is a class that includes the main method, that sets each Entry() objects data and then modifies it. Also within this program I am attempting to create an array to store each object. The goal is to create a constructor that takes an array of Entry objects and initializes an AddressBook object. Then I must add other methods but I cannot get the constructor and array to link.

Below is the code, I am trying to pass the array Entry[] Contact through the Addressbook constructor and am getting the error at line 7 that its expecting a ; or .class.



<code>
import java.util.Scanner;

public class Addressbook
{
private Entry[] e;

public Addressbook contact = new Addressbook(Entry[] Contact)
{
e = new Entry[1];
for (int i = 0; i <= 1; i++)
{
e[i] = Contact[i];
}
}

public static void main(String[] args)
{
Entry amy = new Entry();
amy.set("Amy", "Angel", "1504 North 18th Street Philadelphia PA 19121", "2155958296", "stupidPrinc$
amy.writeOutput();
amy.set("Amy", "Angel", "202 West 1st Street Los Angeles CA 90012", "2132375000", "stupidLAgirl@ge$
amy.writeOutput();

Entry brian = new Entry();
brian.set("Brian", "Boring", "1501 Carlisle Street Philadelphia PA 19122", "2158649080", "BoringAs$
brian.writeOutput();
brian.set("Brian", "Boring", "225 Baker Street NOrthwest Atlanta GA 30313", "4045814000", "StillBo$
brian.writeOutput();

Entry[] Contact = new Entry[1];
Contact[0] = amy;
Contact[1] = brian;


Addressbook contact = new Addressbook(Contact);
contact.e[0] = amy;
contact.e[1] = brian;
}
}
</code>

Hello!

I am trying to pass an array as a parameter of a constructor in my program. Entry is a class used to modify the objects amy and brian instance variables. Addressbook is a class that includes the main method, that sets each Entry() objects data and then modifies it. Also within this program I am attempting to create an array to store each object. The goal is to create a constructor that takes an array of Entry objects and initializes an AddressBook object. Then I must add other methods but I cannot get the constructor and array to link.

Below is the code, I am trying to pass the array Entry[] Contact through the Addressbook constructor and am getting the error at line 7 that its expecting a ; or .class.



import java.util.Scanner;

public class Addressbook
{
private Entry[] e;

public Addressbook contact = new Addressbook(Entry[] Contact)
{
e = new Entry[1];
for (int i = 0; i <= 1; i++)
{
e[i] = Contact[i];
}
}

public static void main(String[] args)
{
Entry amy = new Entry();
amy.set("Amy", "Angel", "1504 North 18th Street Philadelphia PA 19121", "2155958296", "stupidPrinc$
amy.writeOutput();
amy.set("Amy", "Angel", "202 West 1st Street Los Angeles CA 90012", "2132375000", "stupidLAgirl@ge$
amy.writeOutput();

Entry brian = new Entry();
brian.set("Brian", "Boring", "1501 Carlisle Street Philadelphia PA 19122", "2158649080", "BoringAs$
brian.writeOutput();
brian.set("Brian", "Boring", "225 Baker Street NOrthwest Atlanta GA 30313", "4045814000", "StillBo$
brian.writeOutput();

Entry[] Contact = new Entry[1];
Contact[0] = amy;
Contact[1] = brian;


Addressbook contact = new Addressbook(Contact);
contact.e[0] = amy;
contact.e[1] = brian;
}
}



Is This A Good Question/Topic? 0
  • +

Replies To: Linking Arrays and Objects through Constructors

#2 darek9576  Icon User is offline

  • D.I.C Lover

Reputation: 198
  • View blog
  • Posts: 1,693
  • Joined: 13-March 10

Re: Linking Arrays and Objects through Constructors

Posted 06 December 2012 - 03:46 PM

Post the Entry class.
Was This Post Helpful? 0
  • +
  • -

#3 RidleyMoose  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 02-December 12

Re: Linking Arrays and Objects through Constructors

Posted 06 December 2012 - 03:51 PM

mport java.util.Scanner;

public class Entry
{

        private String firstName;
        private String lastName;
        private String address;
        private String phone;
        private String email;
//      Entry[] Contact = new Entry[];

        public Entry()
        {
                firstName = "Not Entered";
                lastName = "Not Entered";
                address = "Not Entered";
                phone = "Not Entered";
                email = "Not Entered";

        }

        public Entry(String iFirstName, String iLastName, String iAddress, String iPhone, String iEmail)
        {
                set(iFirstName, iLastName, iAddress, iPhone, iEmail);
        }

        public void set(String nFirstName, String nLastName, String nAddress, String nPhone, String nEmail)
        {
                firstName = nFirstName;
                lastName = nLastName;
                address = nAddress;
                phone = nPhone;
                email = nEmail;
        }

        public void readInput()
        {
                System.out.println("Enter the first name of the contact.");
                System.out.println("Enter the last name of the contact.");
                System.out.println("Enter the address of the contact.");
                System.out.println("Enter the contact's phone number.");
                System.out.println("Enter the email address of the contact.");
        }
 public void writeOutput()
        {       
                System.out.println("Name: " + firstName + " " + lastName);
                System.out.println("Address: " + address);
                System.out.println("Email: " + email);
                System.out.println("Phone: " + phone);
        }       
        
      public String getFirstName()
        {
                return firstName;
        }

        public String getLastName()
        {
                return lastName;
        }
 
        public String getAddress()
        {       
                return address;
        }
        
        public String getEmail()
        {       
                return email;
        }
        
        public String getPhone()
        {       
                return phone;
        }
        public String toString() 
        {
                return ("Name: " + firstName + " " + lastName + " \n" + " Address: " + address + " Email: " +
email + " Phone: " +
phone);  
        }
         
 
}


Was This Post Helpful? 0
  • +
  • -

#4 Kemical  Icon User is offline

  • New D.I.C Head

Reputation: 3
  • View blog
  • Posts: 7
  • Joined: 27-November 12

Re: Linking Arrays and Objects through Constructors

Posted 06 December 2012 - 04:30 PM

Here are some of my corrections I i think it will work, sorry i'm on a machine without javac to try to compile,

Thanks

import java.util.Scanner;

public class Addressbook
{
private Entry[] e;

public Addressbook(Entry[] Contact)
{
e = new Entry[2];
for (int i = 0; i < 2; i++)
{
e[i] = Contact[i];
}
}

public static void main(String[] args)
{
Entry amy = new Entry();
amy.set("Amy", "Angel", "1504 North 18th Street Philadelphia PA 19121", "2155958296", "stupidPrinc$
amy.writeOutput();
amy.set("Amy", "Angel", "202 West 1st Street Los Angeles CA 90012", "2132375000", "stupidLAgirl@ge$
amy.writeOutput();

Entry brian = new Entry();
brian.set("Brian", "Boring", "1501 Carlisle Street Philadelphia PA 19122", "2158649080", "BoringAs$
brian.writeOutput();
brian.set("Brian", "Boring", "225 Baker Street NOrthwest Atlanta GA 30313", "4045814000", "StillBo$
brian.writeOutput();

Entry[] Contact = new Entry[2];
Contact[0] = amy;
Contact[1] = brian;


Addressbook contact = new Addressbook(Contact);
// dont need this the constructor assigns data contact.e[0] = amy;
// dont need this the constructor assigns data contact.e[1] = brian;
}
}



Was This Post Helpful? 0
  • +
  • -

#5 RidleyMoose  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 02-December 12

Re: Linking Arrays and Objects through Constructors

Posted 06 December 2012 - 04:34 PM

Thanks but line seven is still running the same error:

Addressbook.java:7: '.class' expected
public Addressbook contact = new Addressbook(Entry[] Contact)
^
Addressbook.java:7: ';' expected
public Addressbook contact = new Addressbook(Entry[] Contact)
Was This Post Helpful? 0
  • +
  • -

#6 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8342
  • View blog
  • Posts: 31,885
  • Joined: 06-March 08

Re: Linking Arrays and Objects through Constructors

Posted 06 December 2012 - 09:52 PM

ouf... no need to go far

public Addressbook contact = new Addressbook(Entry[] Contact)
{
    e = new Entry[1];
    for (int i = 0; i <= 1; i++) 
    {
        e[i] = Contact[i];
    }
}


so e is a Entry array of 1
and you will try to do

e[0] = Contact[0];
e[1] = Contact[1]; <--- that will generate an Array Out of Bound exception
Was This Post Helpful? 0
  • +
  • -

#7 Kemical  Icon User is offline

  • New D.I.C Head

Reputation: 3
  • View blog
  • Posts: 7
  • Joined: 27-November 12

Re: Linking Arrays and Objects through Constructors

Posted 06 December 2012 - 11:23 PM

I am not sure what you were compiling but there is no Line 7 error in my test,
The errors I am getting here are just truncated method parameters from what you pasted as your code.

Again

Fix your array reference you will need [2] for array initialization,
and look at the changes I made.

I hope this helps,

Below is my compiler errors

chris@slack:~/d/arraypass$ javac *.java                                                                                                                                                        
Addressbook.java:19: unclosed string literal
amy.set("Amy", "Angel", "1504 North 18th Street Philadelphia PA 19121", "2155958296", "stupidPrinc$
                                                                                      ^
Addressbook.java:19: ';' expected
amy.set("Amy", "Angel", "1504 North 18th Street Philadelphia PA 19121", "2155958296", "stupidPrinc$
                                                                                                   ^
Addressbook.java:20: illegal start of expression
amy.writeOutput();
   ^
Addressbook.java:20: ';' expected
amy.writeOutput();
               ^
Addressbook.java:21: unclosed string literal
amy.set("Amy", "Angel", "202 West 1st Street Los Angeles CA 90012", "2132375000", "stupidLAgirl@ge$
                                                                                  ^
Addressbook.java:21: ';' expected
amy.set("Amy", "Angel", "202 West 1st Street Los Angeles CA 90012", "2132375000", "stupidLAgirl@ge$
                                                                                                   ^
Addressbook.java:22: illegal start of expression
amy.writeOutput();
   ^
Addressbook.java:22: ';' expected
amy.writeOutput();
               ^
Addressbook.java:25: unclosed string literal
brian.set("Brian", "Boring", "1501 Carlisle Street Philadelphia PA 19122", "2158649080", "BoringAs$
                                                                                         ^
Addressbook.java:25: ';' expected
brian.set("Brian", "Boring", "1501 Carlisle Street Philadelphia PA 19122", "2158649080", "BoringAs$
                                                                                                   ^
Addressbook.java:26: illegal start of expression
brian.writeOutput();
     ^
Addressbook.java:26: ';' expected
brian.writeOutput();
                 ^
Addressbook.java:27: unclosed string literal
brian.set("Brian", "Boring", "225 Baker Street NOrthwest Atlanta GA 30313", "4045814000", "StillBo$
                                                                                          ^
Addressbook.java:27: ';' expected
brian.set("Brian", "Boring", "225 Baker Street NOrthwest Atlanta GA 30313", "4045814000", "StillBo$
                                                                                                   ^
Addressbook.java:28: illegal start of expression
brian.writeOutput();
     ^
Addressbook.java:28: ';' expected
brian.writeOutput();
                 ^















View PostRidleyMoose, on 06 December 2012 - 04:34 PM, said:

Thanks but line seven is still running the same error:

Addressbook.java:7: '.class' expected
public Addressbook contact = new Addressbook(Entry[] Contact)
^
Addressbook.java:7: ';' expected
public Addressbook contact = new Addressbook(Entry[] Contact)

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1