|
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
|