我目前使用散列圖存儲有關當前帳戶的信息。檢查HashMap密鑰是否存在時出錯
以下是我有一個方法:
HashMap<String, Account> account = new HashMap<String, Account>();
if (Account.validateID(accountID)) {
System.out.println("Account ID added");
Account a = new Account(cl,accountID, sortCode, 0);
account.put(accountID, a); //add to HashMap
}
這似乎很好地工作。然後,在另一種方法,我有:
public void enterTransaction()
{
String tAccountID = JOptionPane.showInputDialog(this,
"Enter valid accountID", "Add Account", 0);
System.out.println("Check if accountID exists: " + account.containsKey(tAccountID)); //testing if accountID exists - currently not working
Date currentDate = new Date();
System.out.println("Date and time of transaction: " + currentDate); //prints the date and time of transaction
}
基本上,我試圖讓這個當我去進入一個交易,它會檢查已輸入的交易帳戶ID等於從的AccountID HashMap(關鍵字)。
我嘗試使用enterTransaction()的線6,以檢查它是否存在。然而,即使當我知道我已經同時輸入了相同的accountID時,它似乎並不工作,並總是說「錯誤」。我也嘗試使用此聲明:
System.out.println(account.get(accountID));
這似乎給我「帳戶@ cb1edc」?
很抱歉的長期問題,這是一個簡單的問題其實只是想我給你所有的信息,我可以。謝謝。
你能告訴你'Account'類嗎? –
'Account'類中有'toString'方法嗎? –
爲什麼它會存在,如果你還沒有把它放在地圖上呢? –