0

Can anyone see what is wrong with my code? I get 0 from return of the calculation.

Created a small calculation on second class and pass the result data to main class, then print.

main class

package javaapplication3;

public class JavaApplication3 {

    public static void main(String[] args) {

        cal bla = new cal();

        bla.getRatio();
        String dCount = String.valueOf(bla.getRatio());

        System.out.print(dCount);

    }
}

second class

package javaapplication3;

public class cal {

    public int total = 11;
    public int count = 2508;
    public int calRatio;

    public void caln () {

        calRatio = count / total;

        System.out.print(calRatio);

    }

    public int getRatio () {
        return (calRatio);
    }

}

PS: By changing bla.getRatio to bla.caln(); worked. I think I've got other projects mixed up. Thanks for the input guys.

4
  • In addition to the answer, you'll want to look at Java Coding Conventions, specifically how to name classes/public methods/etc. Commented Dec 18, 2012 at 15:13
  • Removed the [pass-by-reference] as a) Java doesn't support pass by reference, b) it doesn't relate to the question. Commented Dec 18, 2012 at 15:14
  • 1
    As a general rule, you should never begin your question with "what's wrong with the code?" Commented Dec 18, 2012 at 15:14
  • Sorry guys, I will be more specific next time and accurate. Commented Dec 18, 2012 at 15:21

7 Answers 7

3

You're doing integer division, which truncates the result to an integer.

You need to cast either operand to double.

Sign up to request clarification or add additional context in comments.

2 Comments

Actually he is never calling "caln" which I think it was intended as a constructor (maybe?) so no calculation takes place. "calRatio" is initialized as 0 and it remains like that.
changed int to double and still get 0.0?
3
bla.getRatio();
String dCount = String.valueOf(bla.getRatio());

You never call the caln()-method, so calRatio is 0 forever.

Maybe you meant the following:

bla.caln();
String dCount = String.valueOf(bla.getRatio());

Plus, you try to divide integers. Try this:

public class cal {

    public int total = 11;
    public int count = 2508;
    public double calRatio;

    public void caln () {

        calRatio = count / total;

        System.out.print(calRatio);

    }

    public double getRatio () {
        return calRatio;
    }

}

3 Comments

Not just that, but his method does integer division.
@TimoteeTheCodeMonkee correct, but in this specific case result of count /total equals 228
And in the event where his values are no longer hard coded? Better to teach good programming practices than hacked solutions. :)
1

You never call the "setter" function caln(), so calRatio was never set. So it returns the 0 for calRatio.

Comments

1

replace

public void caln () {

    calRatio = count / total;

    System.out.print(calRatio);

}

by this

public cal () {

    calRatio = count / total;

    System.out.print(calRatio);

}

Comments

0

Try this:

public static void main(String[] args) {

        cal bla = new cal();
        bla.caln();
        String dCount = String.valueOf(bla.getRatio());

        System.out.print(dCount);

    }

Comments

0

I get 0 from return of the calculation.

As you should. 11 / 2508 does an integer division which is 0

If you want a non-zero I suggest changing

    public double getAverage () {
        return (double) total / count;
    }

Normally you divide the total by the count to get the average.

2 Comments

The OP also never calls the setter function caln() so he needs to change that in addition to doing double division.
@NominSim I am assuming the caln() and the field set doesn't really need to be there. This method will work without calling another method first.
-1

It will return 0, always, because you are returning an int type. The result of your division will always be some floating point value, so you need to store it as such, and return it.

public class cal {
    public int total = 11;
    public int count = 2508;
    public double calRatio;
    public void caln() {
        calRatio = (double)count / (double)total;
        System.out.print(calRatio);
    }
}

4 Comments

Sorry, downvoted because division is 2508/11=228, not the contrary
I suspect the OP has the count and total the wrong way around. ;)
Fixed. Small typo, the concept is the same, nonetheless.
Still this doesn't solve his issue, since he is actually not calling the setter function.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.