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++;
}
}
}