alue
I have a Customer class and a Repair class
I have a TreeMap(customerDetails) of type <String, Customer>.
Each Customer object holds a HashMap(repairs) of type<String, Repair>, the String represents the ref number of the repair.
The user of my system types in a ref number, the program searches to see if any Customer has any Repair with this ref number as a value the displays the Customers details and the Repair details for that ref number.

I'm not sure how to go about searching the customerDetails Map and the repair Map to find the Repair mapped to to String refNumber.
Any help appreciated.
Thanks

Recommended Answers

All 14 Replies

Loop thru coustomers, loop thru each customer's repairs looking for the right key value, eg

for (Customer c : customers) {
    for (String repairNo  : c.getRepairs().keySet()) {
       if (repairNo.equals(...)) ... Repair r = c.getRepairs().get(repairNo)

(this code can be optimised, but that's the clearest version to understand)

for(Customer cus : map)

this line gives me thi error,
for each not applicable to expression type
required; array or java.lang.Iterable
found java.util.Map"

Is map a map, list, collection, array filled with (only) objects of the type Customer?

map is a Map.................

a Map exists out of an Object and a Key, you are treating it as a list or array which has only the Object as element.
that's your problem.

I was illustrating the way to solve your problem, not giving actual complete code. You just need to loop through all your Customers, and given your Map you will find those in map.values()
ps When I used ... on line 3, that's not real Java syntax :-)

Ok, so now at this line

for(Customer c : aMap.values())

I get this error

"Incompatible types
required: rbdeskapp.Customer
found: java.lang.Object"

aMaps values are Customer objects

Can anyone shed some light?
Thanks

Can you post the definition for the aMap variable.

I have read a TreeeMap to a file in another method, then read that file back it this methon, casting what I read back to a TreeMap

File file = new File("reboot_customer_details.txt");  
FileInputStream f = new FileInputStream(file);  
ObjectInputStream s = new ObjectInputStream(f);  
aMap = (TreeMap)s.readObject();
s.close();

Sorry I forgot to show you this.
I created a Map in the methood like this

Map aMap = new TreeMap<String, Customer>();

Can you post the definition of aMap.
A definition has the datatype followed by the variable name. The first 3 lines of your post define the variables: file, f and s. aMap is not defined, It is assigned a value.

Is this what you mean?

Map aMap = new TreeMap<String, Customer>();

The definition of aMap (Map aMap) is without any type and defaults to Object. That's why the compiler says:

found: java.lang.Object"

Give it a definition:
Map<K, V> aMap

with the correct types for K and V

Forgot to update this.
Solved, thanks for all your help gys.......

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.