-1

arraylist で accountno を使用してアカウント名を返しますか? アカウント番号を検索するときにアカウント名を返すのに問題があります。ユーザーがアカウント フィールドにアカウント番号を入力すると、そのアカウント番号に関連付けられたアカウント名がアカウント名フィールドに表示されます。

private void txt_wdaccountnumberActionPerformed(java.awt.event.ActionEvent evt) {
for(BankAccount account: list){
    if(account.getAccountNo().equals(txt_wdaccountnumber.getText())){
    txt_wdaccountname.setText(account.getAccountName());
        System.out.println(txt_wdaccountname);
        return;

アカウントの追加:

private void btnSaveAActionPerformed(java.awt.event.ActionEvent evt) {                                         
    BankAccount account = new BankAccount();

    ButtonGroup bg = new ButtonGroup();
    bg.add(rad_savings);
    bg.add(rad_checking);
    account.setAccountName(txt_accountname.getText());
    account.setAccountNo(txt_accountnumber.getText());
    account.setBalance(Double.parseDouble(txt_initialbalance.getText()));
    list.add(account);
    String fileName = "bank.txt";
    FileWriter file = null;
    try {
        file = new FileWriter(fileName, true);
        PrintWriter pw = new PrintWriter(file);
        for (BankAccount str : list) {
            pw.println(str);
           }

        pw.flush();
        pw.println("\n");
4

1 に答える 1

1

if 条件が true の場合、目的の BankAccount が account 変数に保持されているので、それを使用します。

for(BankAccount account: list){
  if(account.getAccountNo().equals(txt_wdaccountnumber.getText())){

    // the account variable will now hold the BankAccount of interest

    // let's test to see if we are getting to this spot

    String accountName = account.getName();
    System.out.println("inside if block. accountName: " + accountName);

    txt_wdaccountname.setText(accountName);
    return;
  }
}

アカウントの名前を表示する JTextField の名前、またはその名前を取得するための BankAccount のメソッドがわからないことに注意してください。そのため、上記のコードで推測しましたが、アイデアを得て、そこから先に進む必要があります。

于 2013-06-03T02:03:43.820 に答える