Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    New to the CF scene
    Join Date
    Aug 2005
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Arrays using method?

    Hi,

    I'm very new to Java here, and I was wondering how I could make use of arrays with the aid of method. The below code shows where I'm stuck at.

    For example,


    ------------------------------------------------
    import javax.swing.*;

    public class voting_project { // CLASS start





    public static void main (String args[]) { // MAIN start

    String candidate_name1 = JOptionPane.showInputDialog("Input first candidate's name"); // Prompt for 1st candidate's name
    String candidate_name2 = JOptionPane.showInputDialog("Input second candidate's name"); // Prompt for 2nd candidate's name

    vote(candidate_name1, candidate_name2);


    System.exit (0);
    } // MAIN end






    // ========== VOTE ((method)) start ==========
    public static void vote(String candidateA, String candidateB) {
    //int array_no = 1000; // Declare maximum array number
    int[] vote_no = new int[1000]; // Declare variable for array
    boolean validate_result;
    String vote_input;
    int candidate_no;
    int counter;


    // ========== FOR loop start ==========
    for (counter = 1; counter < 1000; counter++) {

    vote_input = JOptionPane.showInputDialog( // Ask for vote number
    "Enter 1 to vote for " + candidateA + "\n" +
    "Enter 2 to vote for " + candidateB + "\n" +
    "Enter 99 to show the result for voting.");

    candidate_no = Integer.parseInt(vote_input); // Convert to integer

    if (candidate_no == 99) System.exit(0);

    validate_result = validateVote(candidate_no); // Boolean validation

    vote_no[counter] = calculateVote(counter, vote_no);



    System.out.println("Vote number " + counter + "\n\n---");

    }
    // ========== FOR loop end ==========



    }
    // ========== VOTE ((method)) end ==========








    public static boolean validateVote(int intVote) { // VALIDATEVOTE method start
    boolean result = ((intVote == 1) || (intVote == 2) || (intVote == 99));
    return result;
    } // VALIDATEVOTE method end






    public static int calculateVote(int candidate, int[] record) { // CALCULATEVOTE method start

    int total_votes = 1;
    record = new int[1000];
    record[total_votes] = candidate;
    return record[total_votes++];


    } // CALCULATEVOTE method end





    } // CLASS end

  • #2
    Regular Coder martin_narg's Avatar
    Join Date
    Jul 2002
    Location
    Chamonix, France
    Posts
    600
    Thanks
    1
    Thanked 3 Times in 3 Posts
    Hi mate, this is a JavaScript forum, not a Java forum.

    Info

    m_n
    "Cos it's strange isn't it. You stand in the middle of a library and go 'Aaaaaaaaaaaaaaaaggggggghhhhhhh!'
    and everybody just stares at you. But you do the same in an aeroplane, and everybody joins in."
    -Tommy Cooper


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •