1

So I have an enum here:

public enum Party {

    DEMOCRAT, INDEPENDENT, REPUBLICAN
}

and I currently have this, one of three classes:

public class ElectoralCollege {
    public static final String FILE = "Electoral201X.txt";
    private ArrayList <State> stateVotes;
    Random rand = new Random();

    public ElectoralCollege() throws IOException    {

        stateVotes = new ArrayList<State>();
        assignStates();
    }

    public void assignStates() throws IOException   {

        File f = new File(FILE);
        Scanner fReader = new Scanner(f);

        while(fReader.hasNext())    {

            String stateData = fReader.nextLine();
            int stateEnd = stateData.indexOf(" - ");
            String stateName = stateData.substring(0, stateEnd);
            String stateVotes = stateData.substring(stateEnd + 2);
            //System.out.println(stateName + " " + stateVotes);


        }

Here I am reading from a file that has state names and their number of electoral votes as follows "Florida - 29", so that's all figured out.

What I have to do next is use a random object to assign a party to them from my Party enum. Republican and Democrat must have a 2/5 chance of winning...while Independent must have a 1/5 chance. Then I must create a State object (which takes the state name, number of votes, and the party in as parameters) and toss it into that arraylist. Most likely going to use a for each loop for that, just need to do some more research on that.

My question is how do I use this random object rand with a set probability for those three parties, and execute it? Anyone got any ideas?

EDIT: Bottom line is: How do I implement a 2/5 and a 1/5 probability for those three Parties, and then call the random object to give me a party based on those probabilities?

AFTER mre's Answer, I did this:

Random rand = new Random();
    List<Party> parties = Arrays.asList(Party.DEMOCRAT, Party.DEMOCRAT, Party.REPUBLICAN, Party.REPUBLICAN, Party.INDEPENDENT);

and a little later on....

public void assignStates() throws IOException {

File f = new File(FILE);
Scanner fReader = new Scanner(f);

while(fReader.hasNext())    {

    String stateData = fReader.nextLine();
    int stateEnd = stateData.indexOf(" - ");
    String stateName = stateData.substring(0, stateEnd);
    String numVote = stateData.substring(stateEnd + 2);

    Party winner = parties.get(rand.nextInt(5));
    //System.out.println(stateName + " " + numVote + " " + winner);

    State voteInfo = new State(stateName, Integer.parseInt(numVote.trim()), winner);
    stateVotes.add(voteInfo);

}

}

Answered, new question : Using a foreach loop to add values from an arraylist, and then print them using accessors

Community
  • 1
  • 1
Stax
  • 39
  • 7
  • 1
    Are you wondering how to generate a 2/5 probability? Or how to create a probability statically i.e. fixed for program run but executed once? Or how to use random #s at all? Or how to assign random # to enum value? – djechlin Feb 24 '13 at 03:44
  • How to generate a 2/5 and a 1/5 probability, AND how to execute that Random object (syntax wise). – Stax Feb 24 '13 at 03:54
  • IMO you should not be invoking `assignStates()` in the constructor, given what it does.. – mre Feb 24 '13 at 04:00
  • Thanks mre, didn't see that haha – Stax Feb 24 '13 at 04:06
  • you could try something like this: http://stackoverflow.com/questions/5269250/random-value-from-enum-with-probability/5269896#5269896 – Ray Tayek Feb 24 '13 at 05:18
  • A continuation of this...had to make a new question for space... http://stackoverflow.com/questions/15048973/using-a-foreach-loop-to-add-values-from-an-arraylist-and-then-print-them-using – Stax Feb 24 '13 at 06:03

1 Answers1

1

have a collection of 5 Party instances, where 2 are DEMOCRAT, 2 are REPUBLICAN, and 1 is an INDEPENDENT, and then use the random number generator to generate a random index (i.e. 0-4) for accessing e.g.,

import java.util.Arrays;
import java.util.List;
import java.util.Random;

public class Demo 
{
    public static void main(String[] args) 
    {
       Random r = new Random();
       List<Party> parties = Arrays.asList(Party.DEMOCRAT, Party.DEMOCRAT, Party.REPUBLICAN, Party.REPUBLICAN, Party.INDEPENDENT);

       System.out.println(parties.get(r.nextInt(parties.size())));
    }

    enum Party
    {
        DEMOCRAT,
        REPUBLICAN,
        INDEPENDENT;
    }
}
mre
  • 43,520
  • 33
  • 120
  • 170
  • 1
    +1 A similar approach is shown [here](http://stackoverflow.com/a/2343296/230513). – trashgod Feb 24 '13 at 04:05
  • However, then I get a duplicate field error for Party.DEMOCRAT and Party.REPUBLICAN... Although I can't believe I did not think of that! Thanks :) If I get that duplicate error am I doing something wrong tho? xD – Stax Feb 24 '13 at 04:09
  • 1
    Or are you saying make an array of 5 party instances xD right? I can't think tonight lol. I stupidly tried to just add another DEMOCRAT and REPUBLICAN to the enum. *facepalm* – Stax Feb 24 '13 at 04:11
  • @Stax, Yeah, I'm saying make a list of those 5 party instances. Please see my example! – mre Feb 24 '13 at 04:12
  • @trashgod, +1 very nice example. – mre Feb 24 '13 at 04:13
  • @mre Ahhhhhh, Thankyou! So then when I use the randomizer it will give me the index, and I can just use something like if statements to turn that into a string? Like setting a String variable to Democrat if another variable = 1? – Stax Feb 24 '13 at 04:16
  • @Stax, I'm not sure I follow. Can you update your question with an explanation and maybe some code? – mre Feb 24 '13 at 04:17
  • @mre check update, thanks again I think thats good. Any more advice? :) – Stax Feb 24 '13 at 04:31
  • @Stax, there's a whitespace in your `numVote`. You should probably `trim` it so `Integer.parseInt` can parse it correctly. for instance, `Integer.parseInt(numVote.trim())` – mre Feb 24 '13 at 04:39
  • @mre ahh, the whitespace after the '-'. How would I trim it? I'm not familiar with that function. – Stax Feb 24 '13 at 04:40
  • @Stax, `Integer.parseInt(numVote.trim())` – mre Feb 24 '13 at 04:41
  • 1
    @mre thankyou so much! Works fine, now I just need to figure out how to print it out in numerous ways for the next part. Gonna try it by myself first haha! – Stax Feb 24 '13 at 04:58