This week's giveaway is in the EJB and other Java EE Technologies forum.
We're giving away four copies of EJB 3 in Action and have Debu Panda, Reza Rahman, Ryan Cuprak, and Michael Remijan on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes Error in calculation Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Error in calculation" Watch "Error in calculation" New topic
Author

Error in calculation

Lucas Stelljes
Greenhorn

Joined: Jan 31, 2011
Posts: 6
Greetings,

I am working on a program to calculate the GPA of a student using a "While" Loop. I have the program written to near completion, and it runs, but I have an error.
Whenever I try to calculate the GPA, no matter how "high" or "low" the student's theoretical GPA should be, it always retains a result of 0.0. I am unable to figure out what I have mistyped/left out.

Can anyone help or suggest anything, or how to refine the program?
Thanks, Lucas

Tom Reilly
Rancher

Joined: Jun 01, 2010
Posts: 618
You only set gpa in one place. When you calulate gpa, you divide qualPts by totalCredits. What happens if qualPts is zero? What happens if totalCredits is zero? Here's a suggestion. Everywhere you set qualPts to a value, print its value. Every where you set totalCredits, print its value. Is the output what you expect?
Lucas Stelljes
Greenhorn

Joined: Jan 31, 2011
Posts: 6
Thank you for the quick reply.

So this is how I understand your comments: I need to define gpa in more then one place in order for it to "calculate" properly?

Also, I think I found my error. I have qualPts set = 0 at the beginning of the program. During the Loop when I try to calculate qualPts again, I multiply (numgrade + credits)*qualPts. Am I getting a 0.0 for my gpa calculation because it's being multiplied by 0, thus gaining a net result of 0?

Thanks,
-Lucas
Tom Reilly
Rancher

Joined: Jun 01, 2010
Posts: 618
So this is how I understand your comments: I need to define gpa in more then one place in order for it to "calculate" properly?

No. I pointed out "one place" so that you recognise there is only one place that could set gpa to zero.
Also, I think I found my error. I have qualPts set = 0 at the beginning of the program. During the Loop when I try to calculate qualPts again, I multiply (numgrade + credits)*qualPts. Am I getting a 0.0 for my gpa calculation because it's being multiplied by 0, thus gaining a net result of 0?

That's one possibility. Another is that (numgrade + credits) sums to zero. Try adding more print statements to see which is correct.
Lucas Stelljes
Greenhorn

Joined: Jan 31, 2011
Posts: 6
I have found out that it's my assignment of "double qualPts = 0" that is resulting in the 0.0 GPA result.

What do I have to do to change this? I thought about creating a "private static double" method, but didn't think it was necessary. Is it, or is something else required?

-Lucas
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 10912
    
  12

what is 'qualPts' supposed to represent? I can't tell from your code or the variable name what it is supposed to be. perhaps you need to initialize it to something other than 1?

Perhaps you need an if statement in there to do something different if it is equal to zero?

What would your 'private static double' method DO exactly?

Tell us in English (not java) what this variable represents, and how you are supposed to calculate it, and perhaps we can give you better guidance.


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Error in calculation
 
Similar Threads
Actually I'm lost
Having "Cannot find symbol" and "Class, Interface, Enum Expected" Errors still
Inheritance error?
Help with GPA calculator program
Not passing the value