• Advertisement
Sign in to follow this  

One more question!

This topic is 3033 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
Check the types of sum and votes.

Then read the start of this.

EDIT: Or just read _fastcall's post :)

Share this post


Link to post
Share on other sites
**masters!**
haha Thanks! a bunch guys that worked perfectly and i understand it!
I'm really new at programming but am loving it, even tho the little errors get annoying I want to just keep programming!

Thanks!

only thing left is to format to two decimal places and I know how to do that! :)

Share this post


Link to post
Share on other sites
jsym that is a great attitude.

if you truely have love for programming, never let anyone tell you anything is beyond your capability to learn.

in the end, things aren't THAT complicated and you have as much as anyone else was born with to become a great programmer.

so keep at it and good luck (:

Share this post


Link to post
Share on other sites
One thing I would like to suggest that will help a lot in situations like this:

Enter test phrases throughout your code to see if you get to them. (I dont know Java so this will be in C++ but youll get the idea) For example:

If I have the below code:
[SOURCE]

//Here is a loop section that is giving me trouble:

for(i = 0; i < 100; i++)
{
cout<< "TESTING TO SEE IF MY PROGRAM MAKES IT INSIDE THE LOOP

//blah blah extra stuff here
}
[/SOURCE]


I know its not the best example but its a good way to debug when you are having trouble. I use it all the time when something isn't calculating write. I stick a print to screen command in there and if the command executes, the block its in usually executes.

I hope that made sense and didn't confuse you more :)

Share this post


Link to post
Share on other sites
What exactly are you expecting that System.exit(0); which is outside of any method to do?

Share this post


Link to post
Share on other sites
Quote:
Original post by Chrono1081
I know its not the best example but its a good way to debug when you are having trouble. I use it all the time when something isn't calculating write. I stick a print to screen command in there and if the command executes, the block its in usually executes.


C'mon, printf debugging? It's not exactly rocket science to set a breakpoint. Teach good debugging practices from the start.

Share this post


Link to post
Share on other sites
if you have to debug retail code, or don't have a good debugger, or various other reasons, knowing how to printf debug is a good skill as well.

Share this post


Link to post
Share on other sites
Thanks to all.
the System.exit(0); was a mistake by the way, i dont know why i had that in there but i got it all figured out about 2 in the morning :P
and i got an A :)

thanks once again and I can't wait to learn code more in detail!

Share this post


Link to post
Share on other sites
Quote:
Original post by Atrix256
if you have to debug retail code, or don't have a good debugger, or various other reasons, knowing how to printf debug is a good skill as well.


... yes, if you're working in the stone ages, knowing how to use a stone hammer is useful.

Lack of proper debugging technique is one of the things most often missed by beginner programmers. It's something that has a constant, practical impact on their ability to get things done. Promoting printf debugging on a beginners forum is recklessly backwards.

Share this post


Link to post
Share on other sites
Quote:
Original post by Telastyn
Quote:
Original post by Atrix256
if you have to debug retail code, or don't have a good debugger, or various other reasons, knowing how to printf debug is a good skill as well.


... yes, if you're working in the stone ages, knowing how to use a stone hammer is useful.

Lack of proper debugging technique is one of the things most often missed by beginner programmers. It's something that has a constant, practical impact on their ability to get things done. Promoting printf debugging on a beginners forum is recklessly backwards.


You have a point but printf debugging isnt just for the stone ages, it's a good skill to have and completely useful in specific situations (retail only bugs for one).

Not to mention it's language agnostic, which helps when you are programming in languages which don't have IDEs or debuggers such as PHP, javascript, lua, custom scripting languages and others.

I agree with you but i'm just sayin, printf debugging is useful and completely necessary at times!

Share this post


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

  • Advertisement