-3

I was trying to create a Fahrenheit to Celcius converter. I used the following code for it:

package Kap3;
import javax.swing.*;

public class Upp32{
    public static void main(String[] arg){
        char d = '\u00B0';
        String F = JOptionPane.showInputDialog("Write the temperature in F"+d);
        int f = Integer.parseInt(F);
        int C = (f-32)*(5/9);
        // TEST - JOptionPane.showMessageDialog(null, f);
        JOptionPane.showMessageDialog(null, f + "" + d
                + " Degrees Fahrenheit" + "\n Equals to"
                + "\n" + C + d + " Degrees Celcius");
    }
}

The problem lays here: JOptionPane.showMessageDialog(null, f + d + " Degrees Fahrenheit"

Somehow it changes the number of Farenheit inputed at the beginning and I don't know why. However if I add +""+ just after f then it goes away, but I would love to know why it happens.

thank you.

(On other notes, I hope this was a correct way to post something. Got banned for 2 days for my lats thread.)

BlackNetworkBit
  • 768
  • 11
  • 21
Aaravos
  • 1
  • 3
  • Java is to Javascript as Pain is to Painting, or Ham is to Hamster. They are completely different. It is highly recommended that aspiring coders try to learn the name of the language they're attempting to write code in. When you post a question, please tag it appropriately. – CertainPerformance Aug 31 '19 at 09:49
  • `f` is an integral value, `d` is an integral value and are added together. – QBrute Aug 31 '19 at 09:52

1 Answers1

0

I was able to solve it! Apparently there was something wrong with my multiplication.

package Kap3;
import javax.swing.*;

public class Upp32{
    public static void main(String[] arg){

        char d = '\u00B0';
        String F = JOptionPane.showInputDialog("Write the temperature in F"+d);

        double f  = (int) (Double.parseDouble(F));
        double C = (int) ((f - 32) * 5/9);
                    // TEST - JOptionPane.showMessageDialog(null, f);
            JOptionPane.showMessageDialog(null, f+ ""+d + " Degrees Fahrenheit"
                    + "\n Equals"
                    + "\n" + C + d + " Degrees Celcius");
                }
}

Changed double C = (int) ((f - 32) * 5/9);

Aaravos
  • 1
  • 3