gpt4 book ai didi

java - 无法将值传递给 Java 中的 ArrayList

转载 作者:行者123 更新时间:2023-11-30 07:10:03 25 4
gpt4 key购买 nike

我有一个汽车拍卖的投标人类,当用户单击提交按钮时,我似乎无法将 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);

}

问候

最佳答案

当您应该传递 checkCSV 时,您将 csv 作为最后一个参数传递。我强烈建议您摆脱那个庞大的构造函数并改用 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);
}
}

关于java - 无法将值传递给 Java 中的 ArrayList,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22541952/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com