Hi All,

I have been working on this all night. I have to convert degrees to celcius and vice versa. I keep having 2 errors. "possible loss of precision". I am new to java, but also I think I am having serious mental issues at this point.

Here is my code:

import java.util.*;
    public class weather
    {
        static Scanner console = new Scanner(System.in);

    public static void main(String[] arguments)
    {

        float fah = 86;
        System.out.println(fah + " %.2f degrees Fahrenheit is ...");   // To convert Fahrenheit into Celsius

        fah = (fah - 32);    // Begin by subtracting 32
        fah = (fah / 9.0);   // Divide the answer by 9
        fah = (fah * 5.0);   // Multiply that answer by 5

        System.out.println(fah + " degrees Celsius\n");

        float cel = 33;
        System.out.println(cel + " degrees Celsius is ...");  // To convert Celsius into Fahrenheit

                cel = cel * 9;           // Begin by multiplying it by 9
                cel = cel / 5;           // Divide the answer by 5
                cel = cel + 32;          // Add 32 to the answer
                System.out.println(cel + " degrees Fahrenheit");
   }
 }

Thanks

Recommended Answers

All 10 Replies

you have to either remove the decimal places from 'fah' or put a 'f' after the decimal places
eg.
fah = (fah / 9.0f);
fah = (fah * 5.0f);

Member Avatar for iamthwee

And what happens if you declare it as double instead of float?

Hi amthewee,

At this point, i tried the double, but I must not have done something right with it as well.

i will admit I am very confused at this point

Thanks

Hi Bad Aapples,
ok I removed the decimal and repalced it with f...that is a good thing. I no longer have the error.

Thanks

Hi iamthwee,

What if I want to have the user enter the temp themselves. i realize with me having the fah = 86 nad cel =33, that only allows that particular conversion.

Hpw do I code that?

Thanks

Hi amthwee,

the link does not work. any other suggestions?

thanks

float fah = console.nextFloat();

Hi titanium,

Thanks so much. that was the majority of my problem.

To everyone, I thank all of you very very much. Please know, when I ask a question, I have searched through my book, but I am very new to java and programming. I never want anyone to just give me the code...I can't learn that way. However, I do need an explanation, telling me where, and why. For you veteran programmers....one day I hope to be able to do it

Thanks

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.