• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Iterating through a MAP that is in a different class.

 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm trying to iterate through a MAP that is in a different class than the one I'm doing the iterating in.

In class: Company, I have defined a Map:

private Map<String, Insured> insureds = new HashMap<String, Insured>();

This is a MAP of Insureds that in turn contains as SET of Coverages that in turn contains a SET of Products.

In class: CoverageReport, I am using this statement to try to iterate through the Insured MAP to get information of the Insureds, their Coverages and in turn their Products:

for(Map.Entry<String, Insured> entry : insureds.entrySet()) {
Insured insureds = entry.getValue();
InsRpt.append(entry.toString()).append("\n");
InsRpt.append("Coverage: ");


the interpreter is telling me that the variable: insureds cannot be resolved.

Can anyone tell me what I'm doing wrong?

 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to JavaRanch.

insureds is a private member variable of your class Company. Because it is private, it's only visible inside that class. What you can do is add a getter method to class Company, and call that from class CoverageReport. But note that there you will need to have a Company object to call the method on. For example:

 
Sheriff
Posts: 22784
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I would like to make one modification to that:
That way the user can iterate through the map, and get values for keys, but not change the map in any way.
 
Joe Solis
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for the advice
 
Joe Solis
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Continuing with this project, I am now trying to get information from a Class called Coverage.

As I mentioned earlier, this project uses a MAP of Insureds, which contains a SET of Coverages, which in turn contains a SET of Products. Inside Class: Coverage, there is a METHOD called: totalProductCost that iterates through the SET of products and accumulates the cost of the products for each coverage.

What I am trying to do in a Class called "CoverageReport" is to "Drill-down" into Class: Coverage to get at this Method.

In the Java class I'm taking, we learned how to access collection data on a single level, but not how to "drill-down" into multiple levels.


Thanks.


 
Ranch Hand
Posts: 174
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Any chance for SSCCE? I see so much text with so little info. TellTheDetails! How would you describe 'drill-down'? How is your class CoverageReport related to Coverage ... is that where you're maintaining a Map of Coverage objects?
reply
    Bookmark Topic Watch Topic
  • New Topic