无法将值传递给Java中的ArrayList

时间:2014-03-20 18:26:16

标签: java class arraylist

我有一个汽车竞价的竞价者类,当用户点击提交按钮时,似乎无法将jtextfield值传递到数组中。投标人类是:

package abc;

import java.util.ArrayList;


public class Bidder extends User{
private String firstName, lastName, street, city, postcode, tel, eMail, cardMake, cardNo, expDate;
private int houseNo, csvNo;

public Bidder(){

}

public Bidder(String UN, String UP, int UT, String FN, String LN, int HN,
        String S, String C, String P, String T, String EM, String CM, String CN, String ED, int CSV ){
super (UN, UP, UT);

firstName = FN;
lastName = LN;
houseNo = HN;
street = S;
city = C;
postcode = P;
tel = T;
eMail = EM;
cardMake = CM;
cardNo = CN;
expDate = ED;
csvNo = CSV;
}
static ArrayList<Bidder> BidderArray = new ArrayList<Bidder>();


}

我尝试传递给数组的值与按下提交按钮时的值相同如下:

 private void submitActionPerformed(java.awt.event.ActionEvent evt) {     

    boolean validEntries = true;


    String checkUserName = userName.getText();
    if (checkUserName.equals(""))
    {
        validEntries = false;
        userName.setBackground(Color.red);
    }
    String checkPassword = password.getText();
    if (checkPassword.equals(""))
    {
        validEntries = false;
        password.setBackground(Color.red);
    }
    try{
    int checkUserType = Integer.parseInt(userType.getText());
    }
    catch (Exception error)
    {
        validEntries = false;
        userType.setBackground(Color.red);
    }
    String checkFirstName = firstName.getText();
    if (checkFirstName.equals(""))
    {
        validEntries = false;
        firstName.setBackground(Color.red);
    }
    String checkLastName = lastName.getText();
    if (checkLastName.equals(""))
    {
        validEntries = false;
        lastName.setBackground(Color.red);
    }
    try
    {
        int checkHouseNo = Integer.parseInt(houseNo.getText());
    }
    catch (Exception error)
    {
        validEntries = false;
        houseNo.setBackground(Color.red);
    }
    String checkStreet = street.getText();
    if (checkStreet.equals(""))
    {
        validEntries = false;
        street.setBackground(Color.red);

    }
    String checkCity = city.getText();
    if (checkCity.equals(""))
    {
        validEntries = false;
        city.setBackground(Color.red);

    }
    String checkPostcode = postcode.getText();
    if (checkPostcode.equals(""))
    {
        validEntries = false;
        postcode.setBackground(Color.red);

    }
    String checkTel = tel.getText();
    if (checkTel.equals(""))
    {
        validEntries = false;
        tel.setBackground(Color.red);

    }
    String checkEMail = eMail.getText();
    if (checkEMail.equals(""))
    {
        validEntries = false;
        eMail.setBackground(Color.red);

    }
    String checkCardType = cardType.getText();
    if (checkCardType.equals(""))
    {
        validEntries = false;
        cardType.setBackground(Color.red);

    }
    String checkCardNo = cardNo.getText();
    if (checkCardNo.equals(""))
    {
        validEntries = false;
        cardNo.setBackground(Color.red);

    }
    String checkExpDate = expDate.getText();
    if (checkExpDate.equals(""))
    {
        validEntries = false;
        expDate.setBackground(Color.red);

    }
    try
    {
        int checkCSV = Integer.parseInt(csv.getText());
    }
    catch (Exception error)
    {
        validEntries = false;
        csv.setBackground(Color.red);
    }

    Bidder bidder2 = new Bidder(userName, password, userType, firstName, lastName, houseNo, 
            street, city, postcode, tel, eMail, cardType, cardNo, expDate, csv);
    Bidder.BidderArray.add(bidder2);

}                                 

此致

2 个答案:

答案 0 :(得分:2)

当您传递csv时,您将checkCSV作为最后一个参数传递。我强烈建议你摆脱那个庞大的构造函数,而不是使用setter。它将使代码更易于维护,并将大大有助于避免这些错误。

该类可能看起来像这样:

public class Bidder extends User{

    private String firstName, lastName, street, city, postcode, tel, eMail, cardMake, cardNo, expDate;
    private int houseNo, csvNo;

    public Bidder(){

    }

    public void setFirstName(String name) {
        this.firstName = name;
    }

    public void setLastPassword(String password) {
        this.lastName = password;
    }

    // etc. for all the fields.

    public void setCSV(int csv) {
        this.csvNo = csv;
    }

    // Setters for userName, userPassword, and userType go in class User

    static ArrayList<Bidder> BidderArray = new ArrayList<Bidder>();

}

你会像这样使用它:

private void submitActionPerformed(java.awt.event.ActionEvent evt) {     

    boolean validEntries = true;
    String sVal;
    int iVal;
    Bidder bidder2 = new Bidder();

    sVal = userName.getText();
    if (sVal.equals(""))
    {
        validEntries = false;
        userName.setBackground(Color.red);
    } else {
        bidder2.setUserName(sVal);
    }
    sVal = password.getText();
    if (sVal.equals(""))
    {
        validEntries = false;
        password.setBackground(Color.red);
    } else {
        bidder2.setUserPassword(sVal);
    }

    // for an int value:
    try{
        iVal = Integer.parseInt(csv.getText());
        bidder2.setCSV(iVal);
    }
    catch (Exception error)
    {
        validEntries = false;
        userType.setBackground(Color.red);
    }

    // etc. for all the other fields

    if (validEntries) {
        Bidder.BidderArray.add(bidder2);
    }
}

答案 1 :(得分:2)

idder bidder2 = new Bidder(userName, password, userType, firstName, lastName, houseNo, 
            street, city, postcode, tel, eMail, cardType, cardNo, expDate, csv);

应该是,检查最后一个参数,

idder bidder2 = new Bidder(userName, password, userType, firstName, lastName, houseNo, 
            street, city, postcode, tel, eMail, cardType, cardNo, expDate, checkCSV );
相关问题