Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Traversals

 
Christopher Goad
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 679
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 679
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I use eclipse so it does let me..

Anyway how would I go about getting that variable?
 
Stuart A. Burkett
Ranch Hand
Posts: 679
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is there any way you could show me what you mean?
 
Stuart A. Burkett
Ranch Hand
Posts: 679
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 48961
60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic