Note that there are some explanatory texts on larger screens.

plurals
  1. POErrors when checking if HashMap key exists
    primarykey
    data
    text
    <p>I am currently using a hashmap to store information about a Current Account.</p> <p>Here is what I have in one method:</p> <pre><code>HashMap&lt;String, Account&gt; account = new HashMap&lt;String, Account&gt;(); 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 } </code></pre> <p>This seems to work fine. Then in another method I have:</p> <pre><code>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 } </code></pre> <p>Basically, i'm trying to make it so that when I go to enter a transaction, it checks that the AccountID that is entered for the transaction is equal to the AccountID from the HashMap (the key).</p> <p>I tried using line 6 of the enterTransaction() to check whether it exists. However, it doesn't seem to work and always says "false" even when I know i have typed in the same accountID both times. I have also tried using this statement:</p> <pre><code>System.out.println(account.get(accountID)); </code></pre> <p>This seems to give me "Account@cb1edc" ?</p> <p>Sorry about the long question, it's a simple question really just thought i'd give you all the information I could. Thanks.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload