Go Back   CodingForums.com > :: Client side development > JavaScript programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 08-20-2005, 07:09 AM   PM User | #1
genyue
New to the CF scene

 
Join Date: Aug 2005
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
genyue is an unknown quantity at this point
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
genyue is offline   Reply With Quote
Old 08-20-2005, 10:26 AM   PM User | #2
martin_narg
Regular Coder

 
martin_narg's Avatar
 
Join Date: Jul 2002
Location: Chamonix, France
Posts: 600
Thanks: 1
Thanked 3 Times in 3 Posts
martin_narg is an unknown quantity at this point
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
martin_narg is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 06:11 PM.


Advertisement
Log in to turn off these ads.