wood burning stoves*
The moose likes Beginning Java and the fly likes array problem Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "array problem" Watch "array problem" New topic
Author

array problem

Ciaran Reid
Greenhorn

Joined: Jun 26, 2010
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
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

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

Joined: Jun 26, 2010
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

Joined: Aug 24, 2011
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
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

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

Joined: Sep 13, 2006
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 ...


Ankur Kapoor

Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 37926
    
  22
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

Joined: Mar 16, 2011
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

Joined: Jun 26, 2010
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

Joined: Jan 05, 2010
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

Joined: Jun 26, 2010
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

Joined: Oct 13, 2005
Posts: 37926
    
  22
And why is every member of that class marked static?
Ciaran Reid
Greenhorn

Joined: Jun 26, 2010
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

Joined: Oct 13, 2005
Posts: 37926
    
  22
Did you actually pay for that book?
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: array problem
 
Similar Threads
And the winners are ...
And the winners are ...
The winners are ....
How would i break this down into seperate metods?
AND THE WINNERS ARE!!!