# Thread: Why is my temp converter program always outputting 0.0?

1. Member Join Date
Oct 2012
Posts
50
Rep Power
0

## 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));  Reply With Quote

2. Moderator   Join Date
Feb 2009
Location
New Zealand
Posts
4,716
Rep Power
18

## 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;`  Reply With Quote

3. Member Join Date
Oct 2012
Posts
50
Rep Power
0

## Re: Why is my temp converter program always outputting 0.0? Originally Posted by pbrockway2 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.  Reply With Quote

4. Member Join Date
Oct 2012
Posts
50
Rep Power
0

## Re: Why is my temp converter program always outputting 0.0? Originally Posted by pbrockway2 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).  Reply With Quote

5. Moderator   Join Date
Feb 2009
Location
New Zealand
Posts
4,716
Rep Power
18

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

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

#### Posting Permissions

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