import javax.swing.JOptionPane;
public class test{
	public static void main(String args[]){
		
		String numT = JOptionPane.showInputDialog(null,"Please enter a value.");
		double num1 = Double.parseDouble(numT);
		
		System.out.println("Fahrenheite = "+num1+"\nCelcius = "+convertFtoC( num1 ));
	}
	
	public static double convertFtoC ( double num1 ){
			
		return ((5/9)*(num1-32));
	}
}

why the value return is zero? hrm... can anyone point out my mistake? thanks

Dani AI

Generated

The zero comes from integer arithmetic: because both 5 and 9 are integer literals the expression that computes the ratio is evaluated with integer division (truncating toward zero) before it’s multiplied by the temperature difference. As pointed out, that’s the root cause, and demonstrates a simple way to force floating-point arithmetic so the ratio isn’t zero.

A few practical improvements that go beyond that single fix:

  • Guard the input from JOptionPane for null (Cancel) and catch NumberFormatException around the parse step so the program won’t crash on bad input.
  • Use clear names: fahrenheit and celsius are easier to read than num1. Capitalize the class name and follow Java camelCase for methods (for example, fahrenheitToCelsius).
  • Format the output to a sensible number of decimals (for example, one or two places) using System.out.printf or DecimalFormat.
  • Run GUI work on the Event Dispatch Thread (SwingUtilities.invokeLater) in real Swing applications.

A couple of extra notes: double arithmetic is fine for temperature conversions in ordinary programs, but binary floating-point has small rounding quirks; BigDecimal can be used if exact decimal arithmetic is required. Test conversions against known points (32°F → 0°C, 212°F → 100°C, -40°F → -40°C) to confirm correctness and rounding.

Quick checklist:

  • Check for null before parsing input.
  • Surround parsing with try/catch for NumberFormatException.
  • Ensure the division is done in floating point (one operand must be a floating literal or cast).
  • Use meaningful names and follow Java naming conventions.
  • Format the printed result for readability.
  • Consider running Swing code on the EDT for larger GUI work.

Recommended Answers

All 2 Replies

the mistake is that you think your calculation results in a floating point number. It doesn't, it results in an integer number.

/*this code shld work...5/9 returns 0 so use 5/9.0 which returns float value*/
import javax.swing.JOptionPane;
public class Test{
public static void main(String args[]){


String numT = JOptionPane.showInputDialog(null,"Please enter a value.");
double num1 = Double.parseDouble(numT);


System.out.println("Fahrenheite = "+num1+"\nCelcius = "+convertFtoC( num1 ));
}


public static double convertFtoC ( double num1 ){


return ((5/9.0)*(num1-32));
}
}
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.