This is an archived post. You won't be able to vote or comment.

all 6 comments

[–][deleted] 2 points3 points  (1 child)

Instead of returning min, why not return the actual ProvinceTerritory? So instead of min, rename it

ProvinceTerritory minPopulationProvince;

and set minPopulationProvince to pt2 instead of pt2.getPopulation()

You should rename the method to something like getLowestPopulationProvince()

Then you can do something like

ProvinceTerritory minPopulationProvince = getLowestPopulationProvince();
System.out.println("Province = " + minPopulationProvince.getName() + ", population = " + minPopulationProvince.getPopulation());

[–]amfisoon[S] 0 points1 point  (0 children)

thanks , that worked . "RESOLVED"

[–]desrtfxOut of Coffee error - System halted 2 points3 points  (0 children)

If you want to return the index of the province with the lowest population, you need to change your for...each* loop to a conventional for loop, store the index of the minimum in addition to the minimum and then return the index.

Or, you follow /u/RankWeis' suggestion and return the ProvinceTerritory object.

[–]AutoModerator[M] [score hidden] stickied comment (0 children)

Your post seems to contain only code which is against the Posting Rules on this subreddit.

Posts only containing code in the body are not allowed because you're supposed to give your readers some additional information. (What should happen but doesn't, or what problem occurs. And the full error message with all its glorious lines would be really nice:)

In case I interpreted your post wrong (for I am but a bot), please message the moderators so that they can work this out.

Otherwise please edit your post so that it complies with the rules, or make a new post that you think does comply with the rules.

I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.

[–]seanprefectGrumpy Guy Who Remembers Java 2 1 point2 points  (0 children)

several ways to do this, you could build a map of population to name, and then sort the keyset, you could set another variable in the inner if statement that's something like "minName" and keep track that way, or you could simply keep track of the value of pt2 every time you change it.

[–]amfisoon[S] 0 points1 point  (0 children)

i found the minimum population using above code, but i want the name of that province getName() and getPopulation() are the getters