# array problem

Ciaran Reid
Greenhorn
Posts: 13
Hi im at my wits end here trying to get this simple array problem sorted and would appreciate some help. I have 2 arrays, one for 'candidates' filled with names. and the other is 'votes' which is filled via scanner. Simple voting. the output is the name of the candidate with the total amount of votes they got. I have the code working perfectly if there is one winner.
But if there is multiple winners the output will be all their names along with the total votes they got to win. and i have tried everything to my knowledge but I am stuck. Im not asking for the code for the answer but I need some help just to know what road to go down.
Sure take a look at my code. I deleted out the rubbish code which wasnt working to print out multiple winners so you can see what it looks like. Thanks in advance.

Paul Clapham
Sheriff
Posts: 21443
33
Ciaran Reid wrote:But if there is multiple winners the output will be all their names along with the total votes they got to win.

No, the code you posted only outputs one winner. Are you sure you posted the right code? This code obviously only outputs one name.

Ciaran Reid
Greenhorn
Posts: 13
Paul Clapham wrote:
Ciaran Reid wrote:But if there is multiple winners the output will be all their names along with the total votes they got to win.

No, the code you posted only outputs one winner. Are you sure you posted the right code? This code obviously only outputs one name.

yea but I dont know how i can output multiple winners. I tried an if statement in the 'largest' method where if votes[largest] = votes[x] etc then print both. but if there is only one winner then this doesnt work

Shinil Mohan
Ranch Hand
Posts: 37
This will be easy to do with a Collection - Map / Tree. You will be able to get the sorted collection, you need to print all the first values or last values based on your order of sorting. Will this work for you.

Paul Clapham
Sheriff
Posts: 21443
33
Ciaran Reid wrote:yea but I dont know how i can output multiple winners.

Ah, I see, when you said it "will" do that you really meant that it "should" do that. So once you have identified the high score, then you have to print all the candidates with that score? That would involve going through the list again, wouldn't it?

Ankur Kapoor
Greenhorn
Posts: 11
Ciaran Reid wrote:Hi im at my wits end here trying to get this simple array problem sorted and would appreciate some help. I have 2 arrays, one for 'candidates' filled with names. and the other is 'votes' which is filled via scanner. Simple voting. the output is the name of the candidate with the total amount of votes they got. I have the code working perfectly if there is one winner.
But if there is multiple winners the output will be all their names along with the total votes they got to win. and i have tried everything to my knowledge but I am stuck. Im not asking for the code for the answer but I need some help just to know what road to go down.
Sure take a look at my code. I deleted out the rubbish code which wasnt working to print out multiple winners so you can see what it looks like. Thanks in advance.

you could do something of this sort:

I have not tested the code ... just get the idea ...

Campbell Ritchie
Sheriff
Posts: 50656
83
In Britain, I heard, when there are multiple candidates for a parliamentary constituency, and there is a tie in the voting, the two "successful" candidates must fight each other with their swords and the survivor is the winner.

Java™ is supposed to be an object-oriented language, and trying to pair up two arrays is by no means good object-oriented programming. You ought to create a Candidate class with name and votes fields. Then you only need one array. You can interrogate the Candidate objects via their getVotes() method and determine the maximum vote by the usual techniques.

ashwin bhawsar
Ranch Hand
Posts: 62
Once you find out the largest vote , you will again have to loop through all votes to check which one is equal to the largest vote and then print the candidates name and the vote that he received.

Ciaran Reid
Greenhorn
Posts: 13
ashwin bhawsar wrote:Once you find out the largest vote , you will again have to loop through all votes to check which one is equal to the largest vote and then print the candidates name and the vote that he received.

YES. yes indeed. its so obvious but could I think how to work it. going to get stuck into this now.
Thanks pal

R Johnson
Greenhorn
Posts: 28
I would

it's probably identical to having two loops but it is easy to read and you don't have to reinvent the wheel. I didn't add any protection for trying to access outside of the array but just thought I'd give an alternative.

cheers

Ciaran Reid
Greenhorn
Posts: 13
Got it sorted. Thanks to everyone who replied, this fried my head!! As the replies says there is easier and more OO ways to do this but im not at that level yet and it was how the question was laid out to be done.

Here is my answer, I am chuffed I got it!!!

Campbell Ritchie
Sheriff
Posts: 50656
83
And why is every member of that class marked static?

Ciaran Reid
Greenhorn
Posts: 13
Campbell Ritchie wrote:And why is every member of that class marked static?

As of yet I don't know why. The book I am learning from hasn't got as detailed as that yet. Its still 2 chapters away.

Campbell Ritchie
Sheriff
Posts: 50656
83
Did you actually pay for that book?