0

Suppose I have a file like this:

Account1 +200
Account2 Holder:John
Account3 -200
Account2 -100

and so on.

I want to be able to query for example "Account1" for money. The Account names can be arbitrary in the text file. How should I go about doing this in Java? I know this sounds suspiciously derpish but for the life of me I can't figure out a way that seems right.

The obvious idea would be to make an ArrayList with objects of type "Account". However then every time you wanted to check an account, you'd have to go through every single item of the ArrayList and carry out getName() to check if it's equal to it, which seems very labour intensive for simply bringing up an object. Is there any way you could somehow convert between string/data and object handles since Java is an interpretive language?

1 Answer 1

2

An obvious solution is to use a HashMap<String, Account> map. check this: https://docs.oracle.com/javase/7/docs/api/java/util/HashMap.html

Then to get Account1 you do: map.get("Account1");

Sign up to request clarification or add additional context in comments.

1 Comment

.+1 for solution. I would prefer HasMap<String,List<Account>> instead where String/key will hold the account name and List/value will hold the repetiting type of similar account

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.