-3

So i have this program, where when I select the area and type, the DP will generate.enter image description here

The problem is, when I select another type (tipe-45 and tipe-54), the result is showed in minus. What is the problem?

if(rb1.isSelected() && t1.isSelected()){
        hargabangunan.setText("90000000");
        luas_tanah_asli.setText("60");}
    else if(rb1.isSelected() && t2.isSelected()){
        hargabangunan.setText("120000000");
        luas_tanah_asli.setText("90");}
    else if(rb1.isSelected() && t3.isSelected()){
        hargabangunan.setText("150000000");
        luas_tanah_asli.setText("110");}
    
    else if(rb2.isSelected() && t1.isSelected()){
        hargabangunan.setText("100000000");
        luas_tanah_asli.setText("70");}
    else if(rb2.isSelected() && t2.isSelected()){
        hargabangunan.setText("130000000");
        luas_tanah_asli.setText("110");}
    else if(rb2.isSelected() && t3.isSelected()){
        hargabangunan.setText("170000000");
        luas_tanah_asli.setText("120");}
   
    else if(rb3.isSelected() && t1.isSelected()){
        hargabangunan.setText("120000000");
        luas_tanah_asli.setText("80");}
    else if(rb3.isSelected() && t2.isSelected()){
        hargabangunan.setText("140000000");
        luas_tanah_asli.setText("120");}
    else if(rb3.isSelected() && t3.isSelected()){
        hargabangunan.setText("200000000");
        luas_tanah_asli.setText("140");}
    
    int htl = hargat*75/100;     
    tst.setText(Integer.toString(htl));
    
    int lta = Integer.parseInt(luas_tanah_asli.getText());
    int ltth = ltt - lta;
    tsa.setText(Integer.toString(ltth));
    
    int hga = Integer.parseInt(hargabangunan.getText());
    int hgaa = hga+lta*hargat+ltth*htl;
    harga.setText(Integer.toString(hgaa));
   
    String hharga;
    hharga = harga.getText();
    int tharga = Integer.parseInt(hharga);
    int dp = tharga*20/100;
    dpf.setText(Integer.toString(dp));
Ole V.V.
  • 81,772
  • 15
  • 137
  • 161
HERI
  • 13
  • 1
  • 5
    You are doing calculations with some rather large numbers there. Have you checked if what you are trying to calculate even fits in the `int` datatype? Maybe the numbers are to big and you are experiencing [integer overflow](https://stackoverflow.com/questions/3001836/how-does-java-handle-integer-underflows-and-overflows-and-how-would-you-check-fo) – OH GOD SPIDERS Oct 04 '22 at 09:02
  • As a quick fix: try changing all your `int` variables to `long`. It might only be needed on the `hga` and `hgaa` variables, but it just might to the trick. – f1sh Oct 04 '22 at 09:22
  • changing to 'long', its work. Guess Im forget about this data type. thanks anyway – HERI Oct 04 '22 at 09:35
  • not enexpected at all... – aran Oct 04 '22 at 21:03

1 Answers1

0

You are using int but you should use long. Integer can't be used in here as the value is just too big and overflows.

Check https://docs.oracle.com/javase/tutorial/java/nutsandbolts/datatypes.html to see available data types and compare maximum values that you can fit into them.

Use Math.exact and similar methods to immediately get an exception when value overflows. See How can I detect integer overflow on 32 bits int?

Klapsa2503
  • 829
  • 10
  • 33