0

I know this question have been asked a lot of times, but I still could not solve the problem. The problem is that I have to store an user input and print out a value.

For example, there are 4 people, person1, person2, person3 and person4. If I vote for person1, the vote number of person1 becomes 1 and the others remain 0. Then if I vote for person2, the vote number of person2 becomes 1 and person1 is also 1.

I can compile the code. But then if I vote for person1, the output becomes 4. and if I then vote for person2, the output of person2 becomes 4 and vote for person1 went back to 0. I am a complete beginner in programming and got stuck at this program for 4 whole days so any help is greatly appreciated. Thank you very much in advance.

import javax.swing.*; // import swing lib for i/o
public class Arrays4
{
public static void main (String[] args)
{
    voteperson();
    voterepeat();
    System.exit(0);
} // end method main

public static int voteperson()
{
    // Initialize String Arrays
    String[] person = new String[4];
    person[0] = "person1";
    person[1] = "person2";
    person[2] = "person3";
    person[3] = "person4";

    // Initialize int Arrays
    int[] votescount = new int[4];
    votescount[0] = 0;
    votescount[1] = 0;
    votescount[2] = 0;
    votescount[3] = 0;

    // Declare String Variables
    String userinput;
    userinput = JOptionPane.showInputDialog
    ("Please tell us which painting you think is the best."+"\n"+
    "Vote 1 "+person[0]+"\n"+
    "Vote 2 "+person[1]+"\n"+
    "Vote 3 "+person[2]+"\n"+
    "Vote 4 "+person[3]);

    int answer = Integer.parseInt(userinput);

    int i;
    for (i=0; i<votescount.length; i++)
    {
        if (answer == 1)
        {
            votescount[0] = votescount[0]+1;
        }
        else if (answer == 2)
        {
            votescount[1] = votescount[1]+1;
        }
        else if (answer == 3)
        {
            votescount[2] = votescount[2]+1;
        }
        else if (answer == 4)
        {
            votescount[3] = votescount[3]+1;
        }
        else
        {

        }
    } // end for loop

    JOptionPane.showMessageDialog
    (null, "The current votes are" + "\n" +
    votescount[0] + " :" + person[0] + "\n" +
    votescount[1] + " :" + person[1] + "\n" + 
    votescount[2] + " :" + person[2] + "\n" +
    votescount[3] + " :" + person[3]);

    return 0;
}
public static void voterepeat()
{
    for (int j=1; j<=4; j++)
    {
        int repeat;
        repeat = voteperson();
        System.out.println(j);
    }
}
}
4
  • 1
    Consider learning how to use a debugger. Commented Oct 12, 2013 at 23:33
  • In java, method names should be camelCased Commented Oct 13, 2013 at 1:15
  • isn't this just one of the ways to syntax? Commented Oct 13, 2013 at 1:38
  • @user2875021 The way you have the methods names will not cause any errors, but the standard is camelCase. It is easier to read and it looks more professional, but you don't have to do it. Commented Oct 13, 2013 at 1:54

3 Answers 3

3

When you do this:

for (i=0; i<votescount.length; i++){...
} // end for loop

The loop happens 4 times. This means that this bit is happening 4 times:

if (answer == 1)
    {
        votescount[0] = votescount[0]+1;
    }

which means the vote count goes up by 4!

Sign up to request clarification or add additional context in comments.

4 Comments

I am still not quite sure why the loop happens 4 times. I am such a noob sorry.
it's because votescount.length is 4. Just remove the loop and it'll work :)
what if I need to store the votecount for another turn of vote? sorry for disturbing you again.
@user2875021 Make it a field of the class.
0

get rid of your for loop:

for (i=0; i<votescount.length; i++)

and make persons and votescount global and static.

This is the updated code:

import javax.swing.*; // import swing lib for i/o
public class Arrays4
{
     static String[] person = new String[4];//these have been made global and static
     static int[] votescount = new int[4];
    public static void main (String[] args)
    {
        // Initialize String Arrays
        person[0] = "person1";//these have been moved so that it is only called once
        person[1] = "person2";
        person[2] = "person3";
        person[3] = "person4";
        // Initialize int Arrays

        votescount[0] = 0;
        votescount[1] = 0;
        votescount[2] = 0;
        votescount[3] = 0;


        voteperson();
        voterepeat();
        System.exit(0);
    } // end method main

    public static int voteperson()
    {
        // Declare String Variables
        String userinput;
        userinput = JOptionPane.showInputDialog
        ("Please tell us which painting you think is the best."+"\n"+
        "Vote 1 "+person[0]+"\n"+
        "Vote 2 "+person[1]+"\n"+
        "Vote 3 "+person[2]+"\n"+
        "Vote 4 "+person[3]);

        int answer = Integer.parseInt(userinput);
        System.out.println(answer);
        int i;


            if (answer == 1)
            {
                votescount[0] = votescount[0]+1;
            }
            else if (answer == 2)
            {
                votescount[1] = votescount[1]+1;
            }
            else if (answer == 3)
            {
                votescount[2] = votescount[2]+1;
            }
            else if (answer == 4)
            {
                votescount[3] = votescount[3]+1;
            }
            else
            {

            }


        JOptionPane.showMessageDialog
        (null, "The current votes are" + "\n" +
        votescount[0] + " :" + person[0] + "\n" +
        votescount[1] + " :" + person[1] + "\n" + 
        votescount[2] + " :" + person[2] + "\n" +
        votescount[3] + " :" + person[3]);

        return 0;
    }
    public static void voterepeat()
    {
        for (int j=1; j<=4; j++)
        {
            int repeat;
            repeat = voteperson();
            System.out.println(j);
        }
    }
}

2 Comments

I would like to ask why making both arrays global and static could make the whole program works? because just now I am figuring how to make a field of class
@user2875021 well the arrays were reset every time you entered voteperson(). They need to be static because static methods, like main, can only access static fields and methods.
0

First you do,

int[] votescount = new int[4];

then, you do

for (i=0; i<votescount.length; i++)
    {
}

So, that loop iterates 4 times. and inside the loop, you do,

if (answer == 1)
    {
        votescount[0] = votescount[0]+1;
    }

and that's why, your count is up by 4!

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.