Ask Experts Questions for FREE Help !
Ask
    albear's Avatar
    albear Posts: 1,594, Reputation: 222
    Ultra Member
     
    #1

    Nov 13, 2007, 12:47 PM
    huge program, and I'm stuck and its frustrating me so
    right OK, it doesn't like the String winner = (whatever) bits at the bottom and I don't know why, please can anybody help me... PLEASE!!

    import java.util.*;
    class MyProblem
    {
    public static void main ( String [] args)



    {

    int [] theNums = new int [5];

    Scanner kybd = new Scanner(System.in);

    for (int I = 0; I < 5 ; ++i)
    {
    System.out.print("Enter your vote:");
    System.out.print("press 0 for 'candidate 1', 1 for 'candidate 2',");
    System.out.print(" 2 for 'candidate 3', 3 for 'candidate 4'and 4 for 'candidate 5' ");
    theNums[i] = kybd.nextInt();

    }
    int can0=0;
    int can1=0;
    int can2=0;
    int can3=0;
    int can4=0;


    for (int I = 0; I < 5 ; ++i)
    {

    if (theNums[i] == 0)
    {

    can0 += 1;
    }


    if (theNums[i]== 1)
    {

    can1 += 1;
    }


    if (theNums[i] == 2)
    {

    can2 += 1;
    }

    if (theNums[i] == 3)
    {

    can3 += 1;
    }

    if (theNums[i]== 4)
    {

    can4 += 1;
    }

    //if (theNums[i]==-1)
    //{
    //end;
    //}

    }


    if ((can0>can1)&&(can0>can2)&&(can0>can3)&&(can0>can4 ))
    {
    String winner = candidate1;
    }
    else if ((can1>can2)&&(can1>can3)&&(can1>can4))
    {
    String winner = candidate2;
    }
    else if ((can2>can3)&&(can2>can4))
    {
    String winner = candidate3;
    }
    else if (can3>can4)
    {
    String winner = candidate4;
    }
    else
    {
    String winner = candidate5;
    }



    System.out.println("winner is choice" + winner);


    }
    }
    albear's Avatar
    albear Posts: 1,594, Reputation: 222
    Ultra Member
     
    #2

    Nov 13, 2007, 12:53 PM
    Well, this is what command prompt says.
    Attached Images
     
    albear's Avatar
    albear Posts: 1,594, Reputation: 222
    Ultra Member
     
    #3

    Nov 13, 2007, 01:08 PM
    Please...
    albear's Avatar
    albear Posts: 1,594, Reputation: 222
    Ultra Member
     
    #4

    Nov 13, 2007, 03:43 PM
    Can anybody explain to me what it is I am forgetting/missing
    qcumber's Avatar
    qcumber Posts: 5, Reputation: 1
    New Member
     
    #5

    Nov 14, 2007, 08:38 PM
    Hi albear,

    Hope I'm not too late.

    From your code, I'm assuming that candidate1 to candidate5 is a string and not some variables hidden somewhere. If it is, you will have to use


    if ((can0>can1)&&(can0>can2)&&(can0>can3)&&(can0>can4 ))
    {
    String winner = "candidate1";
    }
    else if ((can1>can2)&&(can1>can3)&&(can1>can4))
    {
    String winner = "candidate2";
    }
    else if ((can2>can3)&&(can2>can4))
    {
    String winner = "candidate3";
    }
    else if (can3>can4)
    {
    String winner = "candidate4";
    }
    else
    {
    String winner = "candidate5";
    }

    As for the variable winner, declare the winner String just below

    int [] theNums = new int [5];

    like

    int [] theNums = new int [5];
    Stirng winner = "";

    and then for all the String winner = ";

    and then for all the String winner = " lines... remove String.. e.g.. Winner = " lines ... remove String .. eg. winner = ".

    You should be good after this.
    albear's Avatar
    albear Posts: 1,594, Reputation: 222
    Ultra Member
     
    #6

    Nov 20, 2007, 02:44 PM
    Thank you q, I've been told to use a do-while so I'm going to change it and give that a shot, I've just completed a similar program, so hopefully ill be all right now,

Not your question? Ask your question View similar questions

 

Question Tools Search this Question
Search this Question:

Advanced Search


Check out some similar questions!

Frustrating relationship with my mother [ 4 Answers ]

Many adults expect respect from children but then my mother tells me that she doesn't have to respect me because I'm a child. Is it me or is this logic flawed? Don't I as a person deserve respect? And from no one else at LEAST from the only mother I have? My mother is constantly telling me what...

A frustrating insurance situation. [ 5 Answers ]

Recently my unattended vehicle, parked in a common parking lot, was hit by another vehicle that ultimately fled the scene. There were no witnesses to the incident. The following day I found my car damaged, and sought out to find the vehicle that hit me, hoping they were still parked in the same...

Men are frustrating [ 10 Answers ]

You ask a man to do something for you, something simple like pick up the house and he shoves everything in a corner. It is so frustrating sometimes. Its like pulling teeth to try to get him to help me around the house. He is great with helping me with our son... but you I ask him to clean the house...

Frustrating secret [ 75 Answers ]

Hi I am having a lot of trouble at the moment, and have done for the last 1 and a half years. I am kind of with this guy who I love very much and he loves me too. We have no problems together at all, the only problem is my mum. She doesn't like him because she doesn't think he is good enough for...

Ex g/f frustrating. [ 59 Answers ]

Dears peoples, I split up with my girlfriend of 14 months last week. This also happened in mid march but I lost a bit of dignity then and begged her to give it another go. I no wrealise that I should have left her alone and left her space to herself. She says she never felt that same...


View more questions Search