Question

So I am making a program that will be a GUI and I have a main vector of objects in which I will be pulling the various pieces of information for later use. I am currently trying to fill and it will fill it with no errors for the first drawer, but once I add more it takes the last object entry and fills the drawers with all of them. I don't see what the issue is if someone could help.

The problem occurs in here, there are no specific errors that happen in run time and or while compiling:

    import java.awt.Container;

import java.awt.GridLayout;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.SwingConstants;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import java.io.*;
import java.beans.*;
import java.io.IOException;
import java.io.FileNotFoundException;

public class dogParkProgramMain

{


public static Vector <Donor> infoVector  = new Vector <Donor>();
public static Donor donorInfo = new Donor();

//  public static homeScreen hS;
//  public static rawData rD;

public static void main(String[] args) throws FileNotFoundException
{   
    Scanner donorFile = new Scanner (new FileReader("donorInfo.txt"));
    int x = 0; 
    String tempLine = " ";
    String tempLabel = " ";
    String tempName = " ";
    String tempFName = " ";
    String tempLName = " ";
    String tempEmail = " ";
    String tempAddress = " ";
    String tempNumber = " ";
    int tempWard = 0;
    String tempWardS = " ";
    double tempMem = 0.0;
    String tempMemS = " ";
    double tempDonation = 0.0;
    String tempDonationS = " ";
    String tempComment = " ";

    infoVector.clear();

    while(donorFile.hasNext())
    {
        //Fill First Name
        tempLine = donorFile.nextLine();
        tempFName = tempLine.substring(12, tempLine.length());
        tempFName.replaceAll("\\s","");
        donorInfo.setFName(tempFName);
        System.out.println("First Name " + tempFName);

        //Fill Last Name
        tempLine = donorFile.nextLine();
        tempLName = tempLine.substring(11, tempLine.length());
        tempLName.replaceAll("\\s","");
        donorInfo.setLName(tempLName);
        System.out.println("Last Name " + tempLName);

        //Fill Email
        tempLine = donorFile.nextLine();
        tempEmail = tempLine.substring(6, tempLine.length());
        tempEmail.replaceAll("\\s","");
        donorInfo.setEmail(tempEmail);
        System.out.println("Email " + tempEmail);

        //Fill Address
        tempLine = donorFile.nextLine();
        tempAddress = tempLine.substring(16, tempLine.length());
        tempAddress.replaceAll("\\s","");
        donorInfo.setAddress(tempAddress);
        System.out.println("Address " + tempAddress);

        //Fill Phone Number
        tempLine = donorFile.nextLine();
        tempNumber = tempLine.substring(14, tempLine.length());
        tempNumber.replaceAll("\\s","");
        donorInfo.setPhoneNum(tempNumber);
        System.out.println("Phone Number " + tempNumber);

        //Fill Ward
        tempLine = donorFile.nextLine();
        tempWardS = tempLine.substring(26, tempLine.length());
        tempWardS.replaceAll("\\s","");
        tempWard = Integer.parseInt(tempWardS);
        donorInfo.setWard(tempWard);
        System.out.println("Ward " + tempWard);

        //Fill Membership Type
        tempLine = donorFile.nextLine();
        tempMemS = tempLine.substring(17, tempLine.length());
        tempMemS = tempMemS.substring(0,tempMemS.indexOf(" "));
        tempMemS.replaceAll("\\s","");
        tempMem = Double.parseDouble(tempMemS);
        donorInfo.setMemType(tempMem);
        System.out.println("Membership type " + tempMemS);

        //Fill Donations
        tempLine = donorFile.nextLine();
        tempDonationS = tempLine.substring(21, tempLine.length());
        tempWardS.replaceAll("\\s","");
        tempDonation = Double.parseDouble(tempDonationS);
        donorInfo.setDonation(tempDonation);
        System.out.println("Donation " + tempDonationS);

        //Fill Comments
        tempLine = donorFile.nextLine();
        tempComment = tempLine.substring(10, tempLine.length());
        tempComment.replaceAll("\\s","");
        donorInfo.setComment(tempComment);
        System.out.println("Comment " + tempComment);

        infoVector.add(donorInfo);
        x++;

    }

                System.out.println(infoVector.elementAt(1).getAddress());

//  hS = new homeScreen();
//  rD = new rawData();
}
}

//////////////////////////////////////////////////////////////////////////////////////

Sample data entry from a text file:

First Name: Cyndi 
Last Name: Fordham
Email   cfordham@pls-net.org
Mailing Address 100 S Pearl St. Canandaigua NY 14424
Phone Number:   585-394-3916
What ward do you live in?   3
Membership Type:    1 Dog ($25/year)
Additional Donation: 25
Comments: fdsa
(same format no space, goes on)

//////////////////////////////////////////////////////////////////////////////// My Donor class

public class Donor

{
    /********************************************************************
    vars*/

private String fName;
private String lName;
private String email;
private String address;
private String phoneNum;
private int ward;
private double memType;
private double donation;
private String comment;

/*******************************************************************
methods*/

public Donor ()
{
    fName   = " ";
    lName       = " ";
    email   = " ";
    address     = " ";
    phoneNum = " ";
    ward        = 0;
    memType     = 0.0;
    donation = 0.0;
    comment     = " ";
}

public Donor (String fN, String lN, String e, String a, String pN, int w, double mT, double d, String c)
{
    fName   = fN;
    lName       = lN;
    email   = e;
    address = a;
    phoneNum = pN;
    ward        = w;
    memType     = mT;
    donation = d;
    comment     = c;
}

/************************************************************************************************
GETS*/

public String getFName ()
{
    return fName;
}

public String getlName ()
{
    return lName;
}

public String getEmail ()
{
    return email;
}

public String getAddress()
{
    return address;
}

public String getPhoneNum()
{
    return phoneNum;
}

public int getWard()
{
    return ward;
}

public double getMemType()  
{
    return memType;
}

public double getDonation()
{
    return donation;
}

public String getComment ()
{
    return comment;
}

/*****************************************************************************
sets*/

public void setFName (String fN)
{
    fName = fN;
}

public void setLName (String lN)
{
    lName   = lN;
}

public void setEmail(String e)
{
    email   = e;
}

public void setAddress (String a)
{
    address = a;
}

public void setPhoneNum(String pN)
{
    phoneNum = pN;
}

public void setWard (int w)
{
    ward        = w;
}

public void setMemType(double mT)
{
    memType     = mT;
}

public void setDonation (double d)
{
    donation = d;
}

public void setComment (String c)
{
    comment     = c;
}
}
Was it helpful?

Solution

This is because you have created donorInfo object once. Your loop keeps changing the same object, and then adds a reference to it to the list. That is why all your objects look like the last one added.

To fix, remove the static definition of donorInfo, and make it a local variable that you define and intialize in the loop:

// public static Donor donorInfo = new Donor();
// ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
//      This line needs to be removed

while(donorFile.hasNext()) {
    Donor donorInfo = new Donor();
    ...
}

OTHER TIPS

You always use a single instance of donorInfo. Assign a new instance on each iteration.

while(donorFile.hasNext()) {
    donorInfo = new Donor();
    //...
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top