Jump to content
  • Advertisement
Sign in to follow this  
l jsym l

One more question!

This topic is 3266 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hey, I have recently posting a bunch of threads pretty much just asking for you guys to tell me how to fix my problems and I'm sorry for it. I hate to ask for this again, because I feel that I am a nuisance but here I go. The program that I have been writing and have tons and tons of problems on is almost done, i think :S. I know my formatting is wrong but I can't see how wrong it is because nothing will print. I have no idea why it wont print, I've tried contacting other friends who know JAVA but can't get a hold of anyone so I came to this resort. I hope someone can figure out this problem and once again I understand if anyone doesn't want to help me. My (rough) code is:
import java.util.*;

public class Election
{
	static Scanner console = new Scanner(System.in);
	
	static String[] names = new String[5];
	static int[] votes = new int[5];
	static double[] percents = new double[5];
	
	
	
	public static void main(String[] args)
	{
		String winner;
	
		inputC();
		percent();
		winner = winner();
		print(winner);
				
	}	
	
	System.exit(0);
	
	
	
	//create void method to input names and scores into two separate arrays
	public static void inputC()
	{
	
		String name;
		int vote;
		
		
		//get the names and votes
		for (int i = 0; i < names.length; i++)
			{
				System.out.print("Please enter the candidates last name: ");
				name = console.next();
				names = name;
		
				System.out.print("Please enter the candidates total votes: ");
				vote = console.nextInt();
				votes = vote;
			}
	}
	
	
	
	//create method that calculates the percent of votes recieved
	public static void percent()
	{
		int count = 0;
		int sum = 0;
		//find the total sum
		while(count < votes.length)
		
			sum = sum + votes[count];
			count++;
		
		//find the average
		while (count < percents[count])
		{
			percents[count] = votes[count]/sum;
			count++;
		}	
		
	}
	
	
	
	
	//create a method that finds the winner by total votes
	public static String winner()
	{
		int winnerInt;	
		int maxIndex =  0;
		
		for (int index = 1; index < votes.length; index++)
			if (votes[maxIndex] < votes[index])
				maxIndex = index;
				
		//winnerInt = votes[maxIndex];
		
		return names[maxIndex];
	}
	
	
	
	//create a method that prints out the data in a format as shown on pg. 620, problem 7.
	public static void print(String win)
	{
		System.out.printf("%2d%n", "Candidate" + "Votes Received" + "% of Total Votes"); 
		
		for (int w = 0; w < names.length; w++)
			{
			System.out.printf("%2d%n", names[w] + votes[w] + percents[w]);
			}
			

			
	}
	
	
}
	
Like i said earlier, this code is completely a rough sketch haha. I know my formatting is wrong, but i think my logic is right? It might not be but All i need to know is why its not printing out. I can figure everything else out on my own. Once again im sorry and I thank you all for the help!

Share this post


Link to post
Share on other sites
Advertisement
This looks like an assignment, so I'm only going to guide you to answering the question you specifically asked :)

You have an infinite loop in percent.

Share this post


Link to post
Share on other sites
I figured it all out but one thing. So i'm still working on that.
The problem I'm still having is that the percent method is returning the percent 0.0 to all of the percent array and i dont know why yet. I'm looking hard but can't seem to find it :P...seems like a pretty common process hehe

Share this post


Link to post
Share on other sites
HINT:
What is the value of count before you calculate the average, and after you've found the sum? What is the value of percents[count]? What does while (count < percents[count]) mean?

Share this post


Link to post
Share on other sites
well see i actually switched that code to for loops.
So now I have:

public static void percent()
{
int count;
int sum = 0;
//find the total sum
for (count = 0; count < votes.length; count++)
{
sum = sum + votes[count];
}
//find the average
for (count = 0; count < percents.length; count++)
{
percents[count] = votes[count]/sum;
}

}


I should have stated that before. My apologies.

Share this post


Link to post
Share on other sites
This is a longshot, but I think you need to cast one of the integers in the expression votes[count]/sum to a double, because (int)5/(int)10 is 0 and (double)5/(int)10 is 0.5.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!