2

First time posting here. I am having trouble with a program that requires the use of DecimalFormat. I am an intro to Java college student and we have an assignment to:

Have the user input a temperature in Fahrenheit, have the program convert that to Celsius, print out the original temperature and conversion, use a single loop to increase the Fahrenheit by 10 degrees each time and print out that conversion for 20 increments, and format the Celsius using DecimalFormat for 2 decimal places.

So far I have everything working except, the output does not show the DecimalFormat I set. It only outputs Celsius as 34, 45, 87, etc. It never outputs the decimal places. If I change the DecimalFormat to #.00, it prints out 34.00, 45.00, etc. I would appreciate and assistance you can give. Here is my code so far.

import java.text.DecimalFormat;
import java.util.Scanner;

public class TempConvert {

    public static void main(String[] args) {

        /**
         * Formats celsius to two decimal places.
         */
        DecimalFormat f = new DecimalFormat("##.##");

        /**
         * Declare the variables
         */
        int fahrenheitTemp, count = 0;
        double celsiusTemp;

        /**
         * User inputs temperature.
         */
        Scanner scan = new Scanner(System.in);

        /**
         * Ask user to input a temperature in Fahrenheit.
         */

        System.out.print("Enter a temperature in Fahrenheit between -459 and 212: ");
        fahrenheitTemp = scan.nextInt();

        /**
         * Check to see if temperature is within parameters.
         */
        while (fahrenheitTemp <= -459 || fahrenheitTemp >= 212) {
            System.out.print("Temperature entered is out of Range. Please try again: ");
            fahrenheitTemp = scan.nextInt();
        }

        /**
         * Convert input to celsius.
         */

        celsiusTemp = ((fahrenheitTemp - 32) * 5) / 9;

        /**
         * Loop to add +10 degrees to fahrenheit and print out for 20 cycles.
         */
        while (count < 20) {

            fahrenheitTemp += 10;
            celsiusTemp = ((fahrenheitTemp - 32) * 5) / 9;

            System.out.print("Temperatute in Fahrenheit: " + fahrenheitTemp);
            System.out.println("\tCelsius: " + f.format(celsiusTemp));

            count++;
        }
    }

}
Ordee
  • 35
  • 6
  • @JB Nizet This question about DecimalFormat, not about Integer division: How do you produce a double. May be it is not duplicate? – DontPanic Apr 15 '17 at 18:24
  • The DecimalFormat works fine (or at least, if it doesn't, the OP should specify what he expects it to do instead). The problem is that there is never any decima to print because the code only uses integers, instead of floatig point values. – JB Nizet Apr 15 '17 at 18:27
  • @JB Nizet I know, but he wonders why the formatted output was like 15, 21, 26, 32 and so on, but not as he wanted: 15.00, 21.00 etc. He didn't asked why he got integers. He may use DecimalFormat f = new DecimalFormat("##.00"); pattern instead. Thats, why I thought we can give him an answer. – DontPanic Apr 15 '17 at 18:33
  • That's your understanding of the question. My understanding is that he wonders why there is never any decimal at all. Using ##.00 will display results with .00 always, because all the results will still be wrong. Anyway, the OP now has averything needed to solve all the problems. – JB Nizet Apr 15 '17 at 18:36
  • @JB Nizet ok, may be you're right) – DontPanic Apr 15 '17 at 18:38
  • @Ordee: This question got marked as a duplicate before I was able to post a solution, and I get the feeling that you may be unsatisfied with that. The problem is that you declared `fahrenheitTemp` as an `int` rather than as a `double`. This causes your calculation of `celsiusTemp` to get rounded to the nearest integer, which is why it never gets formatted to 2 decimal places. Change `fahrenheitTemp` to a `double`, use the `nextDouble` method for input, and you’ll get the results you expect. – Joey deVilla Apr 15 '17 at 18:44
  • Thank you very much for the help! – Ordee Apr 15 '17 at 19:45

0 Answers0