Solved

Problems with file input to arraylist

Posted on 2011-03-24
34
380 Views
Last Modified: 2012-06-21
I know that the file I am using can be read by the methods I am using.  The problem I am now having is the contructors (I believe).

After loading the sample data (through the GUI), I request the first record and get only one contact - and that contact is the LAST contact with ONLY the Person object displayed (the rest of the fields are null.

In a nutshell, only one contact from the entire file is loaded.

Here are test results using only the method and a runner to test input

modification for testing (ContactBook.readFromFile(file)

public void readFromFile(String file) throws FileNotFoundException {
        try {
            Scanner scanner = new Scanner(new File(file));
            while (scanner.hasNextLine()) {
                String[] fields = scanner.nextLine().split(":");
//                address = new Address(fields[3],fields[4],fields[5],fields[6]);
//                person = new Person(fields[0],fields[1],fields[2]);
//                friend = new Friend(person, address, fields[7],fields[8]);
                String line = fields[0].toString()+", "+fields[1].toString()+" "+fields[2].toString()+" "+
                        fields[3].toString()+", "+fields[4].toString()+", "+fields[5].toString()+" "+fields[6].toString()+" "+
                        fields[7].toString()+" "+fields[8].toString()+" ";
                System.out.println(line);
            }
            scanner.close();
//            addToList(friend);
        } catch (FileNotFoundException ex) {
            Logger.getLogger(ContactBook.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

Open in new window

Output from reading the file and printing to console:
run:
Last, First MI Street, City, State ZipCode HomePhone CellPhone 
Anderson, Robert M. 19 AnyStreet, AnyCity, AnyTown 12345 (123)456-7890 (987)654-3210 
MacLean, Jerry A. 34th Ave West, #12, Brooklyn, NY 66978 (447)582-9943 (447)221-7735 
LastName, First MI Street, City, State ZipCode HomePhone CellPhone 
Macintosh, Jerry A. 34th Ave West, #12, Brooklyn, NY 66978 (447)582-9943 (447)221-7735 
LName, First MI Street, City, State ZipCode HomePhone CellPhone 
Macgregor, Jerry A. 34th Ave West, #12, Brooklyn, NY 66978 (447)582-9943 (447)221-7735 
TestLast, First MI Street, City, State ZipCode HomePhone CellPhone 
Sherman, Jerry A. 34th Ave West, #12, Brooklyn, NY 66978 (447)582-9943 (447)221-7735 
Shrew, Lucretia W. 44 inches underground, farmers field, KS 55768 none none 
BUILD SUCCESSFUL (total time: 0 seconds)

Open in new window


Runner
package P4.v2_4;

import java.io.FileNotFoundException;

public class AddressBook {

    public static void main(String[] args) throws FileNotFoundException {
//        new AddressBookGUI();
        ContactBook c = new ContactBook();
        c.readFromFile("input.txt");
    }
}

Open in new window


Using the GUI to load the data nets the results problem of only one contact being loaded and of that contact, only the person object is added.
0
Comment
Question by:g_currier
  • 17
  • 9
  • 8
34 Comments
 
LVL 86

Expert Comment

by:CEHJ
ID: 35208534
You're using the wrong delimiter for that file. It should be
String[] fields = scanner.nextLine().split("\\s*,\\s*");

Open in new window

0
 

Author Comment

by:g_currier
ID: 35208593
I'll try that.

 I found part of the problem however.  I moved the addToList() method within the loop and all contacts from the list were added.  However, the addresses are still not part the contact object.
0
 

Author Comment

by:g_currier
ID: 35208606
that was the output, the input file is still colon delimited (i added the commas to the output for clarity only...
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 35208620
Why are those lines commented out in your code?
0
 

Author Comment

by:g_currier
ID: 35208623
Here are the Person, Friend and Address classes (revised)
person class
package P4.v2_4;

public class Person{

    public String lName,fName,mName;

    public Person(){
    }

    public Person(Person person){
        this.fName = person.getFName();
        this.lName = person.getLName();
        this.mName = person.getMName();
    }

    public Person(String lName, String fName, String mName) {
        setLName(lName);
        setFName(fName);
        setMName(mName);
    }

    public String getFName() {
        return fName;
    }

    public String getLName() {
        return lName;
    }

    public String getMName() {
        return mName;
    }

    public void setFName(String fName) {
        this.fName = fName;
    }

    public void setLName(String lName) {
        this.lName = lName;
    }

    public void setMName(String mName) {
        this.mName = mName;
    }

    public String getFullNameString() {
        return lName + "," + fName + " " + mName+"\n";
    }

    @Override
    public String toString() {
        return lName + ":" + fName + ":" + mName;
    }
}

Open in new window

friend class
package P4.v2_4;

public class Friend extends Person {

    public String hPhone, cPhone;
    public Address a = new Address();

    public Friend() {
    }

    public Friend(Person person, Address address) {
        super.lName = person.getLName();
        super.fName = person.getFName();
        super.mName = person.getMName();
        a.streetName = a.getStreetName();
        a.cityName = a.getCityName();
        a.stateName = a.getStateName();
//        a.setAddress(address);
        a.zipCode = a.getZipCode();
    }

    public Friend(String hPhone, String cPhone) {
        sethPhone(hPhone);
        setcPhone(cPhone);
    }

    public Friend(Person person, Address address, String hPhone, String cPhone) {
        super.lName = person.getLName();
        super.fName = person.getFName();
        super.mName = person.getMName();
        a.streetName = a.getStreetName();
        a.cityName = a.getCityName();
        a.stateName = a.getStateName();
        a.zipCode = a.getZipCode();
        sethPhone(hPhone);
        setcPhone(cPhone);

    }

    public String getcPhone() {
        return cPhone;
    }

    public void setcPhone(String cPhone) {
        this.cPhone = cPhone;
    }

    public String gethPhone() {
        return hPhone;
    }

    public void sethPhone(String hPhone) {
        this.hPhone = hPhone;
    }

    public String getFriendString() {
        return getFullNameString() + "\n"
                + a.toString() + "\n"
                + "Home: " + hPhone + "\n"
                + "Mobile:  " + cPhone + "\n";

    }

    @Override
    public String toString() {
        return lName + ":" + fName + ":" + mName + ":" + a.toString() + ":" + hPhone + ":" + cPhone;

    }
}

Open in new window


address class
package P4.v2_4;

public class Address {

    public String streetName, cityName, stateName, zipCode;

    private Address address;

    public Address() {
    }

    public Address(Address address) {
        this.streetName = address.getStreetName();
        this.cityName = address.getCityName();
        this.stateName = address.getCityName();
        this.zipCode = address.getZipCode();
    }

    public Address(String streetName, String cityName, String stateName, String zipCode) {
        setStreetName(streetName);
        setCityName(cityName);
        setStateName(stateName);
        setZipCode(zipCode);
    }

    public Address getAddress(){
        return address;
    }

    public void setAddress(Address address){
        this.address = address;
    }

    public String getCityName() {
        return cityName;
    }

    public void setCityName(String cityName) {
        this.cityName = cityName;
    }

    public String getStateName() {
        return stateName;
    }

    public void setStateName(String stateName) {
        this.stateName = stateName;
    }

    public String getStreetName() {
        return streetName;
    }

    public void setStreetName(String streetName) {
        this.streetName = streetName;
    }

    public String getZipCode() {
        return zipCode;
    }

    public void setZipCode(String zipCode) {
        this.zipCode = zipCode;
    }

    @Override
    public String toString() {
        return streetName + ":" + cityName + ":" + stateName + ":" + zipCode;
    }

    


}

Open in new window


nothing else has changed from the other classes (except the readFromFile method in ContactBook)
0
 

Author Comment

by:g_currier
ID: 35208646
The code in the first message was what I used to test the input (I wanted to see the output on the console to make sure the entir file was read correctly (have some wierd issues with notepad adding hidden carriage return/linefeeds - this was a way to check the cr/lf were in the right places).  This would make sure the scanner wouldn't throw an error at an empty line or find the wrong input in the wrong field.  Testing only (that's why the code is commented out)  It is in use now and all contacts (minus addresses) are fed in to the arraylist.
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 35208684
Only use say 2 lines in the input file and use a different editor. Start with a new file and user WordPad (Start|Run|Write)
0
 

Author Comment

by:g_currier
ID: 35208919
ok
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 35208928
(No copy/paste from the previous file)
0
 

Author Comment

by:g_currier
ID: 35209239
Ok, no more problems with cr/lf.  Why is the Address portion not being fed in?
0
 
LVL 92

Accepted Solution

by:
objects earned 500 total points
ID: 35211049
>             addToList(friend);

addToList should be inside your while loop


            while (scanner.hasNextLine()) {
                String[] fields = scanner.nextLine().split(":");
                address = new Address(fields[3],fields[4],fields[5],fields[6]);
                person = new Person(fields[0],fields[1],fields[2]);
                friend = new Friend(person, address, fields[7],fields[8]);
                addToList(friend);
            }
0
 

Author Comment

by:g_currier
ID: 35211083
I spotted that earlier and changed it accordingly, but the results are still the same.  I'm pretty sure I did something wrong with the Address class or the friend class:

readFromFile():
 
public void readFromFile(String file) throws FileNotFoundException {
        try {
            Scanner scanner = new Scanner(new File(file));
            while (scanner.hasNextLine()) {
                String[] fields = scanner.nextLine().split(":");
                address = new Address(fields[3],fields[4],fields[5],fields[6]);
                person = new Person(fields[0],fields[1],fields[2]);
                friend = new Friend(person, address, fields[7],fields[8]);
                addToList(friend);
            }
            scanner.close();
            
        } catch (FileNotFoundException ex) {
            Logger.getLogger(ContactBook.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

Open in new window


Friend Class
package P4.v2_4;

public class Friend extends Person {

    public String hPhone, cPhone;
    public Address a = new Address();

    public Friend() {
    }

    public Friend(Person person, Address address) {
        super.lName = person.getLName();
        super.fName = person.getFName();
        super.mName = person.getMName();
        a.streetName = a.getStreetName();
        a.cityName = a.getCityName();
        a.stateName = a.getStateName();
//        a.setAddress(address);
        a.zipCode = a.getZipCode();
    }

    public Friend(String hPhone, String cPhone) {
        sethPhone(hPhone);
        setcPhone(cPhone);
    }

    public Friend(Person person, Address address, String hPhone, String cPhone) {
        super.lName = person.getLName();
        super.fName = person.getFName();
        super.mName = person.getMName();
        a.streetName = a.getStreetName();
        a.cityName = a.getCityName();
        a.stateName = a.getStateName();
        a.zipCode = a.getZipCode();
        sethPhone(hPhone);
        setcPhone(cPhone);

    }

    public String getcPhone() {
        return cPhone;
    }

    public void setcPhone(String cPhone) {
        this.cPhone = cPhone;
    }

    public String gethPhone() {
        return hPhone;
    }

    public void sethPhone(String hPhone) {
        this.hPhone = hPhone;
    }

    public String getFriendString() {
        return getFullNameString() + "\n"
                + a.toString() + "\n"
                + "Home: " + hPhone + "\n"
                + "Mobile:  " + cPhone + "\n";
    }

    @Override
    public String toString() {
        return lName + ":" + fName + ":" + mName + ":" + a.toString() + ":" + hPhone + ":" + cPhone;

    }
}

Open in new window


Address Class:
 
package P4.v2_4;

public class Address {

    public String streetName, cityName, stateName, zipCode;

//    private Address address;

    public Address() {
    }

    public Address(Address address) {
        this.streetName = address.getStreetName();
        this.cityName = address.getCityName();
        this.stateName = address.getCityName();
        this.zipCode = address.getZipCode();
    }

    public Address(String streetName, String cityName, String stateName, String zipCode) {
        setStreetName(streetName);
        setCityName(cityName);
        setStateName(stateName);
        setZipCode(zipCode);
    }

//    public Address getAddress(){
//        return address;
//    }

//    public void setAddress(Address address){
//        this.address = address;
//    }

    public String getCityName() {
        return cityName;
    }

    public void setCityName(String cityName) {
        this.cityName = cityName;
    }

    public String getStateName() {
        return stateName;
    }

    public void setStateName(String stateName) {
        this.stateName = stateName;
    }

    public String getStreetName() {
        return streetName;
    }

    public void setStreetName(String streetName) {
        this.streetName = streetName;
    }

    public String getZipCode() {
        return zipCode;
    }

    public void setZipCode(String zipCode) {
        this.zipCode = zipCode;
    }

    @Override
    public String toString() {
        return streetName + ":" + cityName + ":" + stateName + ":" + zipCode;
    }
}

Open in new window

0
 
LVL 92

Expert Comment

by:objects
ID: 35211094
problem could be elsewhere in your code
whats addToList() do
and what returns the first contact
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 35211111
You need to create new instances in the loop
Address address = new Address(fields[3],fields[4],fields[5],fields[6]);// Same for other two objects' creation

Open in new window

0
 

Author Comment

by:g_currier
ID: 35211116
addToList():
 
public void addToList(Friend friend) {
        this.contactList.add(friend);
        setNumContacts();
    }

Open in new window


getFirstContact():
 
public void getFirstContact() {
        if (c.isListEmpty()) {
            abg.setHelpText("There are no contacts in the Address Book.\n"
                    + "Please choose \"Load Sample Contacts\" or\n"
                    + "\"Load Saved Data\" from the \"Edit\" menu.");
        } else {
            iterate = 0;
            ListIterator itr = c.contactList.listIterator(start);
            String line = itr.next().toString();
            Scanner scan = new Scanner(line);
            while (scan.hasNext()) {
                String[] fields = scan.next().split(":");
                abg.setLastName(fields[0]);
                abg.setFirstName(fields[1]);
                abg.setMiddleName(fields[2]);
                abg.setStreetName(fields[3]);
                abg.setCityName(fields[4]);
                abg.setStateName(fields[5]);
                abg.setZipCode(fields[6]);
                abg.setHomePhone(fields[7]);
                abg.setCellPhone(fields[8]);
            }
            scan.close();
            abg.setHelpText("Size of list: " + (c.getSize()) + "\nList index number: " + iterate + "\n\n" + abg.toString());
        }
    }

Open in new window

0
 
LVL 92

Expert Comment

by:objects
ID: 35211136
you can just get the friend from the list directly

Friend friend = contactList.get(0);

(assuming contactList is List<Friend>)

No need to convert to a string and parse
0
 
LVL 92

Assisted Solution

by:objects
objects earned 500 total points
ID: 35211150
public void getFirstContact() {
        if (c.isListEmpty()) {
            abg.setHelpText("There are no contacts in the Address Book.\n"
                    + "Please choose \"Load Sample Contacts\" or\n"
                    + "\"Load Saved Data\" from the \"Edit\" menu.");
        } else {
            Friend friend = (Friend) c.get(0);
            abg.setHelpText("Size of list: " + (c.getSize()) + "\nList index number: " + 0 + "\n\n" + friend);
        }
    }
0
Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 

Author Comment

by:g_currier
ID: 35211230
that stilldoes not make the address fields available

 
public void getFirstContact() {
        if (c.isListEmpty()) {
            abg.setHelpText("There are no contacts in the Address Book.\n"
                    + "Please choose \"Load Sample Contacts\" or\n"
                    + "\"Load Saved Data\" from the \"Edit\" menu.");
        } else {
            Friend f = c.getContact(start);
            abg.setLastName(f.getLName());
            abg.setFirstName(f.getFName());
            abg.setMiddleName(f.getMName());
            abg.setCellPhone(f.getcPhone());
            abg.setHomePhone(f.gethPhone());
            abg.setHelpText("Size of list: " + (c.getSize()) + "\nList index number: " + iterate + "\n\n" + abg.toString());

        }
    }

Open in new window


but it does allow setting the text fields in the GUI.  But the friend Constructor has to be incorrect because I do not have a way to access the address from Friend (I think that's what it is)
0
 
LVL 92

Assisted Solution

by:objects
objects earned 500 total points
ID: 35211272
your not setting the address in abg

>  because I do not have a way to access the address from Friend

add a getAddress() method to the Friend class
0
 

Author Comment

by:g_currier
ID: 35211356
Something like this? (though it doesn't work)

Friend class
 
public Friend(Person person, Address address, String hPhone, String cPhone) {
        super.lName = person.getLName();
        super.fName = person.getFName();
        super.mName = person.getMName();
//        a.streetName = a.getStreetName();
//        a.cityName = a.getCityName();
//        a.stateName = a.getStateName();
//        a.zipCode = a.getZipCode();
        setAddress(a);
        sethPhone(hPhone);
        setcPhone(cPhone);

    }

    public Address getAddress(){
        return a;
    }

    public void setAddress(Address address){
        address.setStreetName(a.streetName);
        address.setCityName(a.cityName);
        address.setStateName(a.stateName);
        address.setZipCode(a.zipCode);
    }

Open in new window

getFirstContact()
 
public Friend(Person person, Address address, String hPhone, String cPhone) {
        super.lName = person.getLName();
        super.fName = person.getFName();
        super.mName = person.getMName();
//        a.streetName = a.getStreetName();
//        a.cityName = a.getCityName();
//        a.stateName = a.getStateName();
//        a.zipCode = a.getZipCode();
        setAddress(a);
        sethPhone(hPhone);
        setcPhone(cPhone);

    }

    public Address getAddress(){
        return a;
    }

    public void setAddress(Address address){
        address.setStreetName(a.streetName);
        address.setCityName(a.cityName);
        address.setStateName(a.stateName);
        address.setZipCode(a.zipCode);
    }

Open in new window

public void getFirstContact() {
        if (c.isListEmpty()) {
            abg.setHelpText("There are no contacts in the Address Book.\n"
                    + "Please choose \"Load Sample Contacts\" or\n"
                    + "\"Load Saved Data\" from the \"Edit\" menu.");
        } else {
            Friend f = c.getContact(start);
            Address a = f.getAddress();
            abg.setLastName(f.getLName());
            abg.setFirstName(f.getFName());
            abg.setMiddleName(f.getMName());
            abg.setStreetName(a.getStreetName());
            abg.setCityName(a.getCityName());
            abg.setStateName(a.getStreetName());
            abg.setZipCode(a.getZipCode());
            abg.setCellPhone(f.getcPhone());
            abg.setHomePhone(f.gethPhone());
            abg.setHelpText("Size of list: " + (c.getSize()) + "\nList index number: " + iterate + "\n\n" + abg.toString());

        }
    }

Open in new window

0
 
LVL 86

Expert Comment

by:CEHJ
ID: 35211380
Did you make the three alterations i mentioned?
0
 

Author Comment

by:g_currier
ID: 35211383
Sorry, got the parameters backward:
 
public Friend(Person person, Address address, String hPhone, String cPhone) {
        super.lName = person.getLName();
        super.fName = person.getFName();
        super.mName = person.getMName();
//        a.streetName = a.getStreetName();
//        a.cityName = a.getCityName();
//        a.stateName = a.getStateName();
//        a.zipCode = a.getZipCode();
        setAddress(address);
        sethPhone(hPhone);
        setcPhone(cPhone);

    }

    public Address getAddress(){
        return a;
    }

    public void setAddress(Address address){
        a.setStreetName(address.streetName);
        a.setCityName(address.cityName);
        a.setStateName(address.stateName);
        a.setZipCode(address.zipCode);
    }

Open in new window

0
 
LVL 92

Assisted Solution

by:objects
objects earned 500 total points
ID: 35211396
yes thats the sort of thing, hows it not working exactly?

> Did you make the three alterations i mentioned?

they aren't needed and won't make any differnce, will just eat up memory unnecessarily
0
 
LVL 92

Expert Comment

by:objects
ID: 35211407
Is getContact() returning the correct contact?
0
 

Author Comment

by:g_currier
ID: 35211409
I did, but then everything diplayed was "null".  With the changed Friend constructor everyhting diplays "out of sync" (friend vs. address)
0
 

Author Comment

by:g_currier
ID: 35211423
getting the first contact with both sets of changes, returns blank text fields (nothing is being set).

reverting back to the old readFromFile allows fileds to be set but the address and friend objects do not correspond
0
 

Author Comment

by:g_currier
ID: 35211425
(when iterating)
0
 
LVL 92

Expert Comment

by:objects
ID: 35211450
> reverting back to the old readFromFile

you shouldn't have need to change that at all (except for moving addToList() inside the loop)

suspect your problem is with getContact()

> (when iterating)

you shouldn't need to iterate at all
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 35211451
Don't make unnecessary changes. That will just lead to problems spiralling. This was your problem:

>>I request the first record and get only one contact - and that contact is the LAST contact

I've just told you how to solve that
0
 

Author Comment

by:g_currier
ID: 35211501
This is what works:

readFromFile():
public void readFromFile(String file) throws FileNotFoundException {
        try {
            Scanner scanner = new Scanner(new File(file));
            while (scanner.hasNextLine()) {
                String[] fields = scanner.nextLine().split(":");
//                Address a = new Address(fields[3],fields[4],fields[5],fields[6]);
//                Person p = new Person(fields[0],fields[1],fields[2]);
//                Friend f = new Friend(person, address, fields[7],fields[8]);
                address = new Address(fields[3],fields[4],fields[5],fields[6]);
                person = new Person(fields[0],fields[1],fields[2]);
                friend = new Friend(person, address, fields[7],fields[8]);
                addToList(friend);
                //Testing
//                String line = fields[0].toString()+", "+fields[1].toString()+" "+fields[2].toString()+" "+
//                        fields[3].toString()+", "+fields[4].toString()+", "+fields[5].toString()+" "+fields[6].toString()+" "+
//                        fields[7].toString()+" "+fields[8].toString()+" ";
//                System.out.println(line);
               
            }
            scanner.close();
            
        } catch (FileNotFoundException ex) {
            Logger.getLogger(ContactBook.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

Open in new window


list "iteration":
public void getFirstContact() {
        iterate = start;
        if (c.isListEmpty()) {
            abg.setHelpText("There are no contacts in the Address Book.\n"
                    + "Please choose \"Load Sample Contacts\" or\n"
                    + "\"Load Saved Data\" from the \"Edit\" menu before iterating through the list.");
        } else {
            Friend f = c.getContact(start);
            Address a = f.getAddress();
            abg.setLastName(f.getLName());
            abg.setFirstName(f.getFName());
            abg.setMiddleName(f.getMName());
            abg.setStreetName(a.getStreetName());
            abg.setCityName(a.getCityName());
            abg.setStateName(a.getStreetName());
            abg.setZipCode(a.getZipCode());
            abg.setCellPhone(f.getcPhone());
            abg.setHomePhone(f.gethPhone());
            abg.setHelpText("Size of list: " + (c.getSize()) + "\nList index number: " + iterate + "\n\n" + abg.toString());
            abg.setHelpText("Size of list: " + (c.getSize()) + "\nList index number: " + iterate + "\n\n" + abg.toString());

        }
    }

    public void getNextContact() {
        if (c.isListEmpty()) {
            abg.setHelpText("There are no contacts in the Address Book.\n"
                    + "Please choose \"Load Sample Contacts\" or\n"
                    + "\"Load Saved Data\" from the \"Edit\" menu before iterating through the list.");
        } else {
            Friend f = c.getContact(iterate);
            Address a = f.getAddress();
            abg.setLastName(f.getLName());
            abg.setFirstName(f.getFName());
            abg.setMiddleName(f.getMName());
            abg.setStreetName(a.getStreetName());
            abg.setCityName(a.getCityName());
            abg.setStateName(a.getStreetName());
            abg.setZipCode(a.getZipCode());
            abg.setCellPhone(f.getcPhone());
            abg.setHomePhone(f.gethPhone());
            abg.setHelpText("Size of list: " + (c.getSize()) + "\nList index number: " + iterate + "\n\n" + abg.toString());
            iterate++;
        }
        abg.setHelpText("Size of list: " + (c.getSize()) + "\nList index number: " + iterate + "\n\n" + abg.toString());
    }

    public void getPreviousContact() {
        if (c.isListEmpty()) {
            abg.setHelpText("There are no contacts in the Address Book.\n"
                    + "Please choose \"Load Sample Contacts\" or\n"
                    + "\"Load Saved Data\" from the \"Edit\" menu before iterating through the list.");
        } else {
            Friend f = c.getContact(iterate);
            Address a = f.getAddress();
            abg.setLastName(f.getLName());
            abg.setFirstName(f.getFName());
            abg.setMiddleName(f.getMName());
            abg.setStreetName(a.getStreetName());
            abg.setCityName(a.getCityName());
            abg.setStateName(a.getStreetName());
            abg.setZipCode(a.getZipCode());
            abg.setCellPhone(f.getcPhone());
            abg.setHomePhone(f.gethPhone());
            abg.setHelpText("Size of list: " + (c.getSize()) + "\nList index number: " + iterate + "\n\n" + abg.toString());
            iterate--;
        }
        abg.setHelpText("Size of list: " + (c.getSize()) + "\nList index number: " + iterate + "\n\n" + abg.toString());

    }

    public void getLastContact() {
        iterate = c.getSize()-1;
        if (c.isListEmpty()) {
            abg.setHelpText("There are no contacts in the Address Book.\n"
                    + "Please choose \"Load Sample Contacts\" or\n"
                    + "\"Load Saved Data\" from the \"Edit\" menu before iterating through the list.");
        } else {
            Friend f = c.getContact(c.getSize()-1);
            Address a = f.getAddress();
            abg.setLastName(f.getLName());
            abg.setFirstName(f.getFName());
            abg.setMiddleName(f.getMName());
            abg.setStreetName(a.getStreetName());
            abg.setCityName(a.getCityName());
            abg.setStateName(a.getStreetName());
            abg.setZipCode(a.getZipCode());
            abg.setCellPhone(f.getcPhone());
            abg.setHomePhone(f.gethPhone());
            abg.setHelpText("Size of list: " + (c.getSize()) + "\nList index number: " + iterate + "\n\n" + abg.toString());

        }
        abg.setHelpText("Size of list: " + (c.getSize()) + "\nList index number: " + iterate + "\n\n" + abg.toString());
    }

Open in new window


I still have to work out how to accurately reflect which index is showing and catch the IndexOutOfBoundsException (when trying to get the previous or next at either end of the list).

It now displays each contact in order from first to last (the ListIterator was giving me a headache anyway).
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 35211605
I mentioned in your other question how to do next/previous

0
 

Author Closing Comment

by:g_currier
ID: 35211641
Thanks for the advice and suggestions
0
 
LVL 92

Expert Comment

by:objects
ID: 35211643
You can check the current index before getting element from list to avoid exception
0
 

Author Comment

by:g_currier
ID: 35211652
Of course, I'm forgetting the simple things now...lol.  I've been working at this for too many hours straight.
0

Featured Post

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Increment alphanumeric sequence 6 80
mapAB Challlenge 35 88
solarwind tftp server 2 31
Java Server Faces parameter pass? 6 22
Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
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 “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.

758 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

21 Experts available now in Live!

Get 1:1 Help Now