I'm making an application that converts units. I cant not for the life of me figure out why this isnt working. The answer returned is 0.0 when I convert from Fahrenheit to Kelvin or vice versa. The K to C conversions work though.

Any insight would be amazing.

This is the Conversion.java class that is called by my ConversionTest.java.

//Temperature is represented with integer values as follows:
//    KELVIN (2)
//    CELSIUS (1)
//    FAHRENHEIT (0)
//############################################################
//  The integer "init" indicates what the initial temperature measurement
//  is and "conv" indicates what to convert to.

public class Conversion {

  
  public static double TempConvert(double initial, int init, int conv) { 
	  switch (init){
	    case 2:
		  switch (conv){
		      case 2: return initial;
		      case 1: return KtoC(initial);
		      case 0: return KtoF(initial);
		      default: return -1;
		  } 
	      
	    case 1:
	      	  switch (conv){
		      case 2: return CtoK(initial);
		      case 1: return initial;
		      case 0: return CtoF(initial);
		      default: return -1;
	      } 

	    case 0:
	    	  switch (conv){
		    	case 2: return FtoK(initial);
		    	case 1: return FtoC(initial);
		    	case 0: return initial;
		    	default: return -1;
	    	} 
	    default: return -1; 
	  }	  
  }
  private static double FtoK(double FarValue){
	return ((FarValue +459.67)*(5/9));}
  private static double FtoC(double FarValue){
	return ((FarValue-32)*(5/9));}
  private static double CtoF(double CelValue){
	return ((CelValue*(9/5)+32));}
  private static double CtoK(double CelValue){
	return (CelValue+273.15);}
  private static double KtoC(double KelValue){
    return (KelValue-273.15);}
  private static double KtoF(double KelValue){
	  return ((KelValue*(5/9))-459.67);}

This the ConversionTest.java code that accesses this class. I get 0.0 as my converted unit every time.

//ConversionTest.java
//testing the class Conversion

import java.util.Scanner;

public class ConversionTest {

  public static void main(String args[]) {
    Scanner input = new Scanner (System.in);
    
    double initialTemp;
    int typeFrom;
    int typeTo;
    double answer;

    System.out.printf("%s", "Enter temperature to convert: ");
    initialTemp = input.nextDouble();
    System.out.println();

    System.out.printf("%s\n%s\n%s\n%s","(0) FAHRENHEIT", "(1) CELSIUS", "(2) KELVIN", 
		    "What type of temp is it: ");
    typeFrom = input.nextInt();
    System.out.println();

    System.out.printf("%s\n%s\n%s\n%s","(0) FAHRENHEIT", "(1) CELSIUS", "(2) KELVIN", 
		    "What type of temp is it: ");
    typeTo = input.nextInt();
    System.out.println();
    

    answer = Conversion.TempConvert(initialTemp, typeFrom, typeTo);

    System.out.printf("%.02f", answer);

  }
}

A lot of your arithmetic uses integers. integer division does not return real values.
1/3 = 0 or in English: 3 goes into 1 zero times. NO REMAINDER. No fractional part.
Change the hardcoded integers to be real numbers.
It would be better to define a CONSTANT to use in the K to F conversion that was a real number.
By real number I mean a double or float.

hahaha wow i feel silly, thank you

Edited 6 Years Ago by pula.alt: n/a

This question has already been answered. Start a new discussion instead.