Farenheit to Celcius

Eddi 0 Tallied Votes 377 Views Share

Hi Dani,
fine Examples (the others too) to learn programming Java, but where is the MyInput.java class?

Thanks
Eddi

package farenheittocelcius;
public class FarenheitToCelcius {
public static void main(String args[])
{
double farenheit, celcius = 0;
System.out.println("\n\tPlease enter temp. in farenheit: ");
farenheit = MyInput.readDouble();
celcius = (5/9)*(farenheit-32);
System.out.println("\n\tThat is " + celcius + " degrees celcius.");
}
}

Dani AI

Generated

The original sample by is a compact demo, but two practical issues were already raised by others: noted that the custom MyInput.java helper is not provided, and correctly identified an integer-division bug that makes the result always 0. Also, normalizing spelling and names helps later readers and searchers (use "Fahrenheit" and "Celsius").

A simple, modern replacement avoids the missing helper and fixes the arithmetic. The example below uses Scanner for input, keeps numeric values as doubles, and formats the output to two decimals:

import java.util.Scanner;

public class FahrenheitToCelsius {
  public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);
    System.out.print("Enter temperature in Fahrenheit: ");
    double fahrenheit = sc.nextDouble();
    sc.close();
    double celsius = (fahrenheit - 32) * 5.0 / 9.0;
    System.out.printf("That is %.2f degrees Celsius.%n", celsius);
  }
}

Key notes and troubleshooting tips:

  • MyInput is not part of standard Java; java.util.Scanner or BufferedReader are preferred for simple console input.
  • The bug discussed by stems from integer division: when both operands are integers the fractional part is discarded, so expressions like 5/9 evaluate to 0. Ensure at least one operand is floating-point (literal with .0 or a d suffix, or cast).
  • Test with canonical points to validate correctness: 32 F -> 0 C, 212 F -> 100 C. Handle non-numeric input with try/catch for InputMismatchException if robustness is needed.
  • For display rounding use printf or DecimalFormat. For finance- or science-grade exactness, consider BigDecimal, but doubles are normally fine for temperature conversions.

For the homework variant mentioned by (Celsius to Fahrenheit), the inverse formula is F = C * 9.0/5.0 + 32 — the same floating-point rule applies.

Dani 5,664 The Queen of DaniWeb Administrator Featured Poster

This program consists of one function in one class. It uses MyInput.java to demonstrate how primitive data types are implemented in Java.

Montrell274 0 Newbie Poster

I had to do this same program for a homework assignment, except we had to do celcius to fahrenheit.

cms271828 2 Junior Poster

Hi, looking at your code, I can see only 0 as answer

When you do 5/9, your dealing with ints, so thats 0.

I think you should do 5.0/9 or 5/9.0 or (double)5/9 or 5/(double)9, or or just 0.55555555555555555

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.