-2

    /* 
    This program will convert Celsius to Fahrenheit and display a table 
    of their Fahrenheit equivalents.
    */
    // ---------1---------2---------3---------4---------5--------6---------7
    // 1234567890123456789012345678901234567890123456789012345678901234567890

    public class CelsiusToFarhenheit
    {
      public static void main(String[] args)
      {
          //Variables
          float fhdeg;
          float celdeg = 0;

      while (celdeg <= 20)
          {
             fhdeg = 1.8 * celdeg + 32;

          }
      if (celdeg <= 20)
          {
             System.out.printf( "    %3d C = %5.1f F\n", celdeg, fhdeg );
             celdeg++;
          }
      }
    }  

I keep getting this error...

CelsiusToFarhenheit.java:22: error: incompatible types: possible lossy conversion from double to float fhdeg = 1.8 * celdeg + 32; ^ 1 error

what I don't get is if i'm initializing them as floats...what's going on? and since I can't get it to initialize, i'm unaware of whether I have any other errors or if it will even print out the table the way I would like.

Drunix
  • 3,313
  • 8
  • 28
  • 50
none
  • 11
  • 5
  • `1.8` change to `1.8f` – Iłya Bursov Feb 26 '15 at 07:16
  • btw, if you run your code, as it is, it will run forever!! Your while loop will never end! – Abhishek Feb 26 '15 at 07:19
  • lol @abhishek I noticed that...when i ran it. what's causing that? and I just got a clearer view on what im supposed to be doing but, I cant get the how of it...so, the table is supposed to print all the numbers from 0-20 if u just give a gist of what im missing, I can attempt to figure it out. – none Feb 26 '15 at 07:59
  • just move the two lines in `if` clause to `while` loop after `fhdeg = 1.8 * celdeg + 32;` line or close the `while` loop after `if`block :) – Abhishek Feb 26 '15 at 08:08
  • I get this now... Exception in thread "main" java.util.IllegalFormatConversionException: d != java.lang.Float at java.util.Formatter$FormatSpecifier.failConversion(Formatter.java:4302) at java.util.Formatter$FormatSpecifier.printInteger(Formatter.java:2793) at java.util.Formatter$FormatSpecifier.print(Formatter.java:2747) at java.util.Formatter.format(Formatter.java:2520) at java.io.PrintStream.format(PrintStream.java:970) at java.io.PrintStream.printf(PrintStream.java:871) at CelsiusToFarhenheit.main(CelsiusToFarhenheit.java:25) – none Feb 26 '15 at 08:13
  • you have to change `%3d` in the format string to `%3f` because `d` is for integers and `celdeg` is a `float`, not an `int`. – Jesper Feb 26 '15 at 09:33

3 Answers3

1

fhdeg = 1.8 * celdeg + 32;

1.8 is a literal of type double. When you multiply a double and a float, the result is going to be a double, which doesn't fit in a float. Use the f suffix to make it a float literal:

fhdeg = 1.8f * celdeg + 32.0f;
Jesper
  • 202,709
  • 46
  • 318
  • 350
  • just note - 32 in original code is integer and promoted to float, so you could omit `.0f` here, but `32.0f` makes intent more clear and thus preferable – Iłya Bursov Feb 26 '15 at 07:21
  • well now. that makes a bit of sense. thank you. – none Feb 26 '15 at 07:23
  • unfortunately, my code doesn't do anything now when it compiles. the instructions honestly weren't too clear on whether a user was supposed to enter a specific value or if the program was supposed to automatically print the conversions...I know it's supposed to go from 0-20 but all i'm getting is...absolutely nothing. – none Feb 26 '15 at 07:27
  • That's because you have a loop `while (celdeg <= 20)` which just keeps going around forever when `celdeg` is `<= 20`. – Jesper Feb 26 '15 at 08:07
  • @jesper so, do I need another if or while loop? and why wont it print. i'm sorry I missed a few sections of the last chapter due to a death in the family last week, and my teacher has yet to respond to the msg I sent him when it happened :/ it's probably something simple that im missing – none Feb 26 '15 at 08:15
  • First get rid of the `while` loop and the `if` statement and run your program. It should print one line. Then learn [how to use `while`](http://docs.oracle.com/javase/tutorial/java/nutsandbolts/while.html) and use it correctly in your program – Jesper Feb 26 '15 at 08:19
  • getting rid of both of those made the program do nothing. Exception in thread "main" java.util.IllegalFormatConversionException: d != java.lang.Float at java.util.Formatter$FormatSpecifier.failConversion(Formatter.java:4302) at java.util.Formatter$FormatSpecifier.printInteger(Formatter.java:2793) at java.util.Formatter$FormatSpecifier.print(Formatter.java:2747) at java.util.Formatter.format(Formatter.java:2520) at java.io.PrintStream.format(PrintStream.java:970) at java.io.PrintStream.printf(PrintStream.java:871) at CelsiusToFarhenheit3.main(CelsiusToFarhenheit3.java:23) – none Feb 26 '15 at 09:06
0

1.8 is a double, and makes the entire expression 1.8 * celdeg + 32 a double. Java refuses the implicit conversion of this double to float upon assignment to fhdeg.

Instead, you can write fhdeg = (float)(1.8 * celdeg + 32);

This leaves the computation as double and only converts to float at the time of assigning the value to fhdeg.

Pascal Cuoq
  • 79,187
  • 7
  • 161
  • 281
  • thank you. unfortunately...now my code doesn't work at all. lol! – none Feb 26 '15 at 07:24
  • @L-G Of course not. The condition of the `while` loop is `celdeg <= 20` and you do not change `celdeg` inside the body of the `while`. But your question is about the compiler warning, so I answered about that. – Pascal Cuoq Feb 26 '15 at 07:27
  • so, in the while statement if I use while (celdeg <= 20) { fhdeg = 1.8 * celdeg + 32; celdeg++; } would that make a difference? I also changed float fhdeg; to float fhdeg = 0; since it wasn't initalizing – none Feb 26 '15 at 07:37
0

In simple words, float (32 bit) can't accommodate double (64 bit). Check java docs, but you can accommodate float in double.

For your program to work, you need to convert 1.8 to float by suffixing f, that is 1.8f or make your variables as double.

Abhishek
  • 6,912
  • 14
  • 59
  • 85