One more question!

Started by
17 comments, last by Atrix256 14 years, 5 months ago
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!
l jsym l
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.
HINT:
There are two bugs in percent(); one of them results in an infinite loop. There is another bug in winner().

Look carefully!

<a href=">

[Edited by - _fastcall on November 3, 2009 11:15:30 PM]
Check your percent() method, specifically the loops.

Edit - beaten.
"All you have to decide is what to do with the time that is given to you." - Gandalf
alright I'll check it out. Thanks guys :)
I really appreciate it
l jsym l
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
l jsym l
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?
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.
l jsym l
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.
Check the types of sum and votes.

Then read the start of this.

EDIT: Or just read _fastcall's post :)

This topic is closed to new replies.

Advertisement