Comparing variables and creating counter - Programmers Heaven

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

Comparing variables and creating counter

syrexsyrex Posts: 16Member
[b][red]This message was edited by syrex at 2005-2-12 4:58:42[/red][/b][hr]
Hey guyz,

I'm stuck on a problem and was hoping you could help me with it.....

I have queried a database table and have put the contents of the result set into a vector and passed that back to a processing method....what I want to do is display similar elements as one with a quantity instead of displaying each seperate element...

i.e. Say I have 4 reams of paper, 3 cartridges and a printer...

I dont want to print out:

Ream of paper
Ream of paper
Ream of paper
Ream of paper
Cartridge
Cartridge
Cartridge
Printer

Instead I want to print out:

Ream of paper 4
Cartridge 3
Printer

how do I search through the vector for elements that are the same and increment a quantity variable?

Thanks a million guyz I really appreciate any help you can give me on this


Comments

  • ronenmronenm Posts: 41Member
    well, I guess you can run through all the Vector elements (meaning from 0 to size(); ) and create a counter for each object.

    paperCounter, cartridgeCounter...etc..

    then inside the for loop running on the Vector do:
    if (object on the i-index is instanceof paper)
    {
    increment the paperCounter;
    }
    if (object on the i-index is instanceof cartridge)
    {
    increment the cartridgeCounter
    }

    and so on...

    instanceof command checks what type of object is in the Vector while the program is running, so it won't just refer to elements of the Vector as Object, but as what they actually are.

    Good Luck!
  • syrexsyrex Posts: 16Member
Sign In or Register to comment.