Results 1 to 5 of 5
  1. #1
    psx2514 is offline Member
    Join Date
    Oct 2012
    Posts
    50
    Rep Power
    0

    Default Why is my temp converter program always outputting 0.0?

    I wrote this very simple temperature converter program that converts Fahrenheit to Celsius. The programs reads the Fahrenheit temp from the user. No matter what value the user inputs, the program always outputs the degrees in Celsius as 0.0. Why?

    Java Code:
    import java.util.Scanner;
    
    public class tempConverter {
      public static void main (String[] args) {
      final int BASE = 32;
      final int CONVERSION_FACTOR = 5/9;
      
      int tempF;
      double tempC;
      Scanner scan = new Scanner (System.in);
      
      
      System.out.println ("Enter a temperature in Fahrenheit, and I will convert it to Celsius.");
      tempF = scan.nextInt();
      tempC = (CONVERSION_FACTOR * (tempF - BASE));
      System.out.println("Your temperature in Celsius is " + tempC);
    }
      
    }
    Last edited by psx2514; 11-03-2012 at 06:35 AM. Reason: changed tempC = (5/9 * (tempF - 32)); to tempC = (CONVERSION_FACTOR * (tempF - BASE));

  2. #2
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default Re: Why is my temp converter program always outputting 0.0?

    Java Code:
    final int CONVERSION_FACTOR = 5/9;
    // ...
    tempC = (5/9 * (tempF - 32));
    I notice you setup a conversion factor, but then later use 5/9. This may be a result of trying all sorts of things because your program was not cooperating.

    One thing to do to see what is going on is to print the value of CONVERSION_FACTOR. (It is better to use this rather than 5/9 in the later expression)

    Java Code:
    import java.util.Scanner;
     
      public class tempConverter {
        public static void main (String[] args) {
        final int BASE = 32;
        final int CONVERSION_FACTOR = 5/9;
        System.out.println("BASE = " + BASE);
        System.out.println("CONVERSION_FACTOR = " + CONVERSION_FACTOR);
       
        int tempF;
        double tempC;
        Scanner scan = new Scanner (System.in);
         
       
        System.out.println ("Enter a temperature in Fahrenheit, and I will convert it to Celsius.");
        tempF = scan.nextInt();
        tempC = (5/9 * (tempF - 32));
        System.out.println("Your temperature in Celsius is " + tempC);
      }
    }
    Remember that / will perform integer division. That is, it divides, truncating the answer and throwing away any remainder.

    This is true even when you assign the result to a double like tempC. The program doesn't "look ahead" to how you are going to use the result later. Rather it does the integer division, gets an unhelpful answer and finally converts that unhelpful integer to an equally unhelpful double value that is assigned to tempC.

    There are various fixes. Personally I'd observe that the conversion factor is a floating point quantity and should be declared that way (ie as double). A single ".0" will force floating point division to be used in an expression:

    Java Code:
    final double CONVERSION_FACTOR = 5.0 / 9;

  3. #3
    psx2514 is offline Member
    Join Date
    Oct 2012
    Posts
    50
    Rep Power
    0

    Default Re: Why is my temp converter program always outputting 0.0?

    Quote Originally Posted by pbrockway2 View Post
    I notice you setup a conversion factor, but then later use 5/9. This may be a result of trying all sorts of things because your program was not cooperating.

    One thing to do to see what is going on is to print the value of CONVERSION_FACTOR. (It is better to use this rather than 5/9 in the later expression)

    Java Code:
    import java.util.Scanner;
     
      public class tempConverter {
        public static void main (String[] args) {
        final int BASE = 32;
        final int CONVERSION_FACTOR = 5/9;
        System.out.println("BASE = " + BASE);
        System.out.println("CONVERSION_FACTOR = " + CONVERSION_FACTOR);
       
        int tempF;
        double tempC;
        Scanner scan = new Scanner (System.in);
         
       
        System.out.println ("Enter a temperature in Fahrenheit, and I will convert it to Celsius.");
        tempF = scan.nextInt();
        tempC = (5/9 * (tempF - 32));
        System.out.println("Your temperature in Celsius is " + tempC);
      }
    }
    Remember that / will perform integer division. That is, it divides, truncating the answer and throwing away any remainder.

    This is true even when you assign the result to a double like tempC. The program doesn't "look ahead" to how you are going to use the result later. Rather it does the integer division, gets an unhelpful answer and finally converts that unhelpful integer to an equally unhelpful double value that is assigned to tempC.

    There are various fixes. Personally I'd observe that the conversion factor is a floating point quantity and should be declared that way (ie as double). A single ".0" will force floating point division to be used in an expression:

    Java Code:
    final double CONVERSION_FACTOR = 5.0 / 9;
    Oops. That's supposed to be tempC = (CONVERSION_FACTOR * (tempF - BASE)); I was messing around with it, and forgot to change it back to that before I posted. I'll edit my OP.

  4. #4
    psx2514 is offline Member
    Join Date
    Oct 2012
    Posts
    50
    Rep Power
    0

    Default Re: Why is my temp converter program always outputting 0.0?

    Quote Originally Posted by pbrockway2 View Post
    I notice you setup a conversion factor, but then later use 5/9. This may be a result of trying all sorts of things because your program was not cooperating.

    One thing to do to see what is going on is to print the value of CONVERSION_FACTOR. (It is better to use this rather than 5/9 in the later expression)

    Java Code:
    import java.util.Scanner;
     
      public class tempConverter {
        public static void main (String[] args) {
        final int BASE = 32;
        final int CONVERSION_FACTOR = 5/9;
        System.out.println("BASE = " + BASE);
        System.out.println("CONVERSION_FACTOR = " + CONVERSION_FACTOR);
       
        int tempF;
        double tempC;
        Scanner scan = new Scanner (System.in);
         
       
        System.out.println ("Enter a temperature in Fahrenheit, and I will convert it to Celsius.");
        tempF = scan.nextInt();
        tempC = (5/9 * (tempF - 32));
        System.out.println("Your temperature in Celsius is " + tempC);
      }
    }
    Remember that / will perform integer division. That is, it divides, truncating the answer and throwing away any remainder.

    This is true even when you assign the result to a double like tempC. The program doesn't "look ahead" to how you are going to use the result later. Rather it does the integer division, gets an unhelpful answer and finally converts that unhelpful integer to an equally unhelpful double value that is assigned to tempC.

    There are various fixes. Personally I'd observe that the conversion factor is a floating point quantity and should be declared that way (ie as double). A single ".0" will force floating point division to be used in an expression:

    Java Code:
    final double CONVERSION_FACTOR = 5.0 / 9;
    Okay. That worked. Thanks (I just changed it to final double CONVERSION_FACTOR = 5./9).

  5. #5
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default Re: Why is my temp converter program always outputting 0.0?

    Great. I assume you understand *why* it worked, which is the main thing.

Similar Threads

  1. Problem with program not outputting properly
    By ericj5555 in forum New To Java
    Replies: 3
    Last Post: 10-16-2012, 02:41 AM
  2. Advice On First Program (binary to decimal converter)
    By HandMeTheBacon in forum New To Java
    Replies: 0
    Last Post: 09-13-2011, 03:35 AM
  3. java.io.FileNotFoundException: \temp\env.out
    By JJones in forum New To Java
    Replies: 3
    Last Post: 05-19-2011, 11:07 PM
  4. Eclipse debug when ant uses temp files
    By aiancu in forum Eclipse
    Replies: 0
    Last Post: 04-21-2009, 03:58 PM
  5. Simple Addition Program Outputting Wrong Value
    By carlodelmundo in forum New To Java
    Replies: 4
    Last Post: 08-05-2008, 03:37 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •