我正在制作一个用户界面让用户插入一个标识号(变量名pNo)。我正在尝试从 JTextfield 读取插入的数字,然后将其传递给另一个应该为用户创建帐户的方法(
createCreditAccount()
)。
问题是方法
createCreditAccount()
没有像它应该的那样返回一个帐号。即使我确定系统中有匹配的 ID 号。
谁能告诉我为什么我没有得到预期的输出。
createCreditAccount()
方法没有返回它应该返回的内容。它返回-1,这在没有具有该 ID 的客户端但我创建了这些客户端的情况下是正确的。我还通过我的主要方法创建了客户和帐户,效果很好。例如:
SubWindowBankContainer.getBank().createCustomer("john", "doe", "7239009");
int accountID =
SubWindowBankContainer.getBank().createCreditAccount("7239009");
String account =
SubWindowBankContainer.getBank().getAccount("7239009", accountID);
上面的这个例子在从 main 方法而不是从 actionlistener 方法调用时工作得很好。
所以我不认为
createCreditAccount()
有什么问题,我认为我的actionPerformed()
有问题;但是什么?
我倾向于
getText()
正在添加额外的空白空间或类似的东西。
public void actionPerformed(ActionEvent creditAccButtonPressed){
if (creditAccButtonPressed.getSource()==pNoButton){
String pNo = pNoTextField.getText();
int creditAccountId = SubWindowBankContainer.getBank().createCreditAccount(pNo);
String Account = SubWindowBankContainer.getBank().getAccount(pNo,creditAccountId);
}
}
这是我调用的方法
public int createCreditAccount(String pNr) {
int accountNr = -1;
int pNoIndex = getIndexForPno(pNr);
if (pNoIndex > -1) {// Om id-nummret finns ...
allCustomers.get(pNoIndex).addToAccountsList(new CreditAccount());
accountNr = Account.getLastAssignedAccountNumber();
}
return accountNr;
}