Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Conversion of Fahrenheit to Celsius and taking input from the user to modify

import java.util.Scanner;

public class FHeightToCelsius {

    public static void main(String[] args)
    //Computes the Celsius equivalent of the inputted Fahrenheit value
    //using the formula C=(F-32)*5/9
    {
        final int BASE = -32;
        final double CONVERSION_FACTOR = 5/9;

        Double message;
        Scanner scan = new Scanner(System.in);
        System.out.println("Enter A Fahrenheit:");
        message = scan.nextDouble();

        double CelsiusTemp;

        CelsiusTemp = (message + BASE) * CONVERSION_FACTOR;

        System.out.println("Your conversion is:" + CelsiusTemp);

    }
}

Math isn’t my strong suit, nor is problem solving (YET!!)
What I also need help with is fleshing out and understanding if this formula is right or correct and how we come about that. I looked up the formula online, but, I’m not sure if I converted it right in code.

I cannot get the math formula to work.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

I’m new to Java, but I probably know the solution to your problem.

The problem is that you are trying to add an int to a String type variable.

I suggest the following changes:

import java.util.Scanner;

public class Main {
    public static void main(String[] args)
    //Computes the Celsius equivalent of the inputted Fahrenheit value
    //using the formula C=(F-32)*5/9
    {
        final int BASE = -32;
        // If you use the variable double you should divide 5.0 / 9
        final double CONVERSION_FACTOR = 5.0 /9;

        Scanner scan = new Scanner(System.in);
        System.out.println("Enter A Fahrenheit:");
        // to avoid the problem I described above use the nextInt() method instead of nextLine()
        int message = scan.nextInt();

        double CelsiusTemp = (message + BASE) * CONVERSION_FACTOR;
        
        // I replaced "message" with the converted value
        System.out.println("Your conversion is:" + CelsiusTemp);
    }
}

I hope I helped.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading