File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Beginning Java and the fly likes A variable is 0 Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "A variable is 0" Watch "A variable is 0" New topic

A variable is 0

Alex MG

Joined: Jan 23, 2005
Posts: 1

I'm new in Java and to learn I'm typing and testing code. I have the following code:

What I want to do is to display the sum of the numbers after dividing them. The problem is the result of r and res variables that always is 0 and I don't know why.

What's wrong?

[ January 23, 2005: Message edited by: Ilja Preuss ]
Ilja Preuss

Joined: Jul 11, 2001
Posts: 14112
Please use code tags in the future - I will add them for you in this case...

The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
Mark Vedder
Ranch Hand

Joined: Dec 17, 2003
Posts: 624

The problem is that you have declared your variables as type int. Therefore when doing the division (this.n/this.d and this.nm/this.nd) the results (0.5 and 0.66666666667) are being cast to type int and being set to of 0 and 0 (since the fraction is truncated off).

Change your variables and parameters n, d, nm, and nd to be of type float and you will get the result of 1.1666667.
Layne Lund
Ranch Hand

Joined: Dec 06, 2001
Posts: 3061
Better yet, don't do the division. I would guess that "fraccion" is your language's equivalent to the English "fraction". A very OO approach to this code, would be to have summ() return a fraccion, rather than a float. You can do this with a single line of code, if you understand the algebra for adding fractions together.

With that said, you should also look into "integer division" as it is called. You should be able to google for this topic to find out why you are getting a 0 as the answer.



[ January 23, 2005: Message edited by: Layne Lund ]
[ January 23, 2005: Message edited by: Layne Lund ]

Java API Documentation
The Java Tutorial
Mark Vedder
Ranch Hand

Joined: Dec 17, 2003
Posts: 624

As another alternative, if you wanted to ensure that your method only took 4 int's as parameters (since it appears you are trying to mimic a pair of fractions), you could cast them to float's as you do the calculation:

This would "prevent" two products of the division from being cast to int's.

Layne made his post while I was typing this second post. I agree that a true object oriented way of doing it would be to have your method return a "fraction" (or faccion) object. If you are not quite ready for that yet in your Java knowledge, give the casting to a float as I suggest here a try.
[ January 23, 2005: Message edited by: Mark Vedder ]
Marilyn de Queiroz

Joined: Jul 22, 2000
Posts: 9059
Hi, Alex MG

Welcome to JavaRanch! Please adjust your display name to meet the JavaRanch Naming Policy. Unfortunately your last name does not comply. You can change it here.


"Yesterday is history, tomorrow is a mystery, and today is a gift; that's why they call it the present." Eleanor Roosevelt
I agree. Here's the link:
subject: A variable is 0
jQuery in Action, 3rd edition