|
||||||||||||||||||
Add Forum to Favorites | Send Topic To a Friend | View Forum FAQ | Track this topic |
Last Thread Next Thread ![]() |
| One more question! |
|
![]() l jsym l Member since: 1/12/2009 From: Reeder |
||||
|
|
||||
| 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[i] = name; System.out.print("Please enter the candidates total votes: "); vote = console.nextInt(); votes[i] = 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! |
||||
|
||||
![]() mattd Member since: 2/26/2000 |
||||
|
|
||||
| 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. |
||||
|
||||
![]() _fastcall Member since: 2/4/2008 From: Salt Lake City, UT, United States |
||||
|
|
||||
| HINT: There are two bugs in percent(); one of them results in an infinite loop. There is another bug in winner(). Look carefully! |
||||
|
||||
![]() Alatar Member since: 3/3/2008 |
||||
|
|
||||
| Check your percent() method, specifically the loops. Edit - beaten. |
||||
|
||||
![]() l jsym l Member since: 1/12/2009 From: Reeder |
||||
|
|
||||
| alright I'll check it out. Thanks guys :) I really appreciate it |
||||
|
||||
![]() l jsym l Member since: 1/12/2009 From: Reeder |
||||
|
|
||||
| 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 |
||||
|
||||
![]() _fastcall Member since: 2/4/2008 From: Salt Lake City, UT, United States |
||||
|
|
||||
| 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? |
||||
|
||||
![]() l jsym l Member since: 1/12/2009 From: Reeder |
||||
|
|
||||
| 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. |
||||
|
||||
![]() _fastcall Member since: 2/4/2008 From: Salt Lake City, UT, United States |
||||
|
|
||||
| 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. |
||||
|
||||
![]() mattd Member since: 2/26/2000 |
||||
|
|
||||
| Check the types of sum and votes. Then read the start of this. EDIT: Or just read _fastcall's post :) |
||||
|
||||
![]() l jsym l Member since: 1/12/2009 From: Reeder |
||||
|
|
||||
| **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! :) |
||||
|
||||
![]() Atrix256 Member since: 4/28/2009 From: Seattle, WA, United States |
||||
|
|
||||
| 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 (: |
||||
|
||||
![]() Chrono1081 Member since: 5/2/2007 From: Altoona, PA, United States |
||||
|
|
||||
| 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 :) |
||||
|
||||
![]() Zahlman Moderator - Game Programming Member since: 1/9/2004 From: Toronto, Canada |
||||
|
|
||||
| What exactly are you expecting that System.exit(0); which is outside of any method to do? |
||||
|
||||
![]() Telastyn GDNet+ Member since: 10/13/2003 From: minneapolis, MN, United States |
||||
|
|
||||
Quote: C'mon, printf debugging? It's not exactly rocket science to set a breakpoint. Teach good debugging practices from the start. |
||||
|
||||
![]() Atrix256 Member since: 4/28/2009 From: Seattle, WA, United States |
||||
|
|
||||
| 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. |
||||
|
||||
![]() l jsym l Member since: 1/12/2009 From: Reeder |
||||
|
|
||||
| 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! |
||||
|
||||
![]() Telastyn GDNet+ Member since: 10/13/2003 From: minneapolis, MN, United States |
||||
|
|
||||
Quote: ... 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. |
||||
|
||||
![]() Atrix256 Member since: 4/28/2009 From: Seattle, WA, United States |
||||
|
|
||||
Quote: 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! |
||||
|
||||
All times are ET (US)![]() |
Last Thread Next Thread ![]() |
|