0

I'm doing a simple program that return the percentage of each number of a set. But for some reason I cannot make the number return correctly except for the last one...

I cannot figure why it always print 0% for each number except the last one.

Here is the code :

public class MainWindow extends JFrame implements ActionListener {
private JButton theButton = new JButton("Calculer sur 100");
private JTextField textField = new JTextField("");
private JTextArea text = new JTextArea("");
private JScrollPane scroller = new JScrollPane(text);

public MainWindow() {
    setLayout(new BorderLayout());
    setTitle("Calculateur de pourcentage");
    setSize(400, 500);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setLocationRelativeTo(null);

    layoutManagement();

    setVisible(true);
}

private void layoutManagement() {

    text.setEditable(false);
    theButton.addActionListener(this);

    getContentPane().add(scroller, BorderLayout.CENTER);
    getContentPane().add(textField, BorderLayout.NORTH);
    getContentPane().add(theButton, BorderLayout.SOUTH);
}

private int checkForNumber()
{
    int numl;
    try{
        numl = Integer.parseInt(textField.getText());
    }catch(NumberFormatException e)
    { 
        text.setText("Please try with number...");
        System.out.println("Error in number format. Returning 0");
        return 0;
    }
    System.out.println("numl = " + numl);
    return numl;
}

private double doMath(int i, int num)
{
    System.out.println("printing result = " + (i / num) * 100);
    return (i / num) * 100;
}

private void print100(int num) {
    for (int i = 1; i < num + 1; i++)
    {
        text.setText(text.getText() + "\n" + i + " : " + doMath(i, checkForNumber()));
    }
}

public void actionPerformed(ActionEvent e) {
    if (e.getSource() == theButton) {
        text.setText("");
        print100(checkForNumber());
    }
}

Any idea?

Chax
  • 944
  • 2
  • 14
  • 33

3 Answers3

1

This is a classic case of integer division. When dividing two integers, the result is an integer. This means that 2/3 is 0. Multiplied by 100, it's still 0.

Instead of (i / num) * 100, use i * 100 / num.

That way, you divide 200 by 3 instead, getting the 66 you're looking for.

Alternatively, you can do the calculation with floating point:

return ((double) i / num) * 100;

By casting, i becomes a double, which means that i / num is calculated as a double rather than an int.

that other guy
  • 101,688
  • 11
  • 135
  • 166
  • @theotherguy Ok i get it. It kinda `Math.floor` all the result from an Integer division.By the way, do you know how to round this number up to two decimal? – Chax Oct 27 '14 at 03:28
  • @Chax It's detailed in [another post](http://stackoverflow.com/a/7747473/1899640). Due to the internal representation, it's better to format the number using a `DecimalFormat` when you display it, rather than trying to return a double with two decimals. Both techniques are described in that answer. – that other guy Oct 27 '14 at 03:49
1

When you put / between integers you get integer division. 3/2 = 1 not 1.5

You could try using doubles in the first place or casting to double like this (double)(3)/2 and get 1.5 but that has it's own problems because double isn't in base 10. If you want perfect calculations of non whole numbers in base 10 check out BigDecimal. I go into why here

Community
  • 1
  • 1
candied_orange
  • 5,855
  • 1
  • 24
  • 57
1

The problem lies in

private double doMath(int i, int num) {

System.out.println("printing result = " + (i / num) * 100);
return (i / num) * 100; 

}

which is integer division, when dividing two integers, the result is an integer (that truncates any decimal value), you might want to parse a value to a float in order to fix that problem as follows:

FROM:

private double doMath(int i, int num) {

System.out.println("printing result = " + (i / num) * 100);
return (i / num) * 100; 

}

TO:

private double doMath(int i, int num) {

System.out.println("printing result = " + (i / (float) num) * 100);
return (i / (float) num) * 100; 

}

That way it makes a floating point division, which returns a floating point number that doesn't truncates the decimals.

Community
  • 1
  • 1
MrJomp
  • 115
  • 6