aspose file tools*
The moose likes Beginning Java and the fly likes Traversals Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of JavaScript Promises Essentials this week in the JavaScript forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Traversals" Watch "Traversals" New topic
Author

Traversals

Christopher Goad
Greenhorn

Joined: Mar 25, 2013
Posts: 27
Having just a few problems. So close to finishing this assignment.

Problem 1: Percent of votes not coming out correctly

Problem 2: Total is not printing

Problem 3: Table allignment is weird.

Here is my code



Stuart A. Burkett
Ranch Hand

Joined: May 30, 2012
Posts: 679
Christopher Goad wrote:
Problem 1: Percent of votes not coming out correctly

Problem 2: Total is not printing

Problem 3: Table allignment is weird.

It might help if you told us what output you are getting and how it differs from what you were expecting.
Christopher Goad
Greenhorn

Joined: Mar 25, 2013
Posts: 27
Okay,

Problem 1: The percent is always 0

Problem 2: I am getting a compiler error

Problem 3: it just looks weird
Stuart A. Burkett
Ranch Hand

Joined: May 30, 2012
Posts: 679
If you're getting a compiler error then you can't be getting any output, because Java code doesn't run until it's been compiled.

In future, it also helps if you include what the compiler error is. Copy and paste the exact error message and indicate which line it is on if it doesn't match the lines numbers in the code you've posted.

Your problem is that the variable total on line 27 is not defined. That's because the one defined on line 32 is local to the printResults method.
Christopher Goad
Greenhorn

Joined: Mar 25, 2013
Posts: 27
I use eclipse so it does let me..

Anyway how would I go about getting that variable?
Stuart A. Burkett
Ranch Hand

Joined: May 30, 2012
Posts: 679
Christopher Goad wrote:I use eclipse so it does let me..

But if the class hasn't compiled correctly, you can't really expect sensible results.

Christopher Goad wrote:Anyway how would I go about getting that variable?

You could return the value from the printResults method and assign it to a variable in your main method or you could just call the getTotals method again on line 27.
Christopher Goad
Greenhorn

Joined: Mar 25, 2013
Posts: 27
Is there any way you could show me what you mean?
Stuart A. Burkett
Ranch Hand

Joined: May 30, 2012
Posts: 679
Christopher Goad wrote:Is there any way you could show me what you mean?

You know how to return a value from a method - you do it in the getName and getVotes methods.
You know how to assign a returned value to a variable - you do it on line 32 of your Testcandidate class.
Give it a try and if you get stuck post your code with a description of what problem you are having.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39773
    
  28
Stuart A. Burkett wrote:If you're getting a compiler error then you can't be getting any output, because Java code doesn't run until it's been compiled. . . .
Actually, you can get a result. You can get whatever result you had from the last version of the class which compiled at all. Even if you know there was an error in it That can be worse even than something which won’t run.
So never use code with compiler errors. I think you are probably better off not using Eclipse at this stage, too.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Traversals