temp converison

Hi, Im trying to create a program that asks the user to enter a code i

Page 1 of 1

5 Replies - 423 Views - Last Post: 19 November 2009 - 06:05 PM Rate Topic: -----

#1 vishalpiratla  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 18-November 09

temp converison

Post icon  Posted 18 November 2009 - 07:03 PM

import java.util.Scanner; 

public class tempconverter {


	
	 
	public static void main(String[] args) {
		final int Base = -32;  
		final double CONVERSION_FACTOR = 5.0 / 9.0;
		int farenheit;
		int CelsiusTemp;
		
		Scanner scan = new Scanner (system.in);		
		System.out.print("Enter the farenheit temperature: ");
		farenheit = scan.nextInt();
		
		CelsiusTemp = farenheit * CONVERSION_FACTOR + Base; 
		
		System.out.println("Celsius Equivalent: " + CelsiusTemp);
	}

}



Is This A Good Question/Topic? 0
  • +

Replies To: temp converison

#2 KYA  Icon User is offline

  • g++ jameson.cpp -o beverage
  • member icon

Reputation: 3101
  • View blog
  • Posts: 19,141
  • Joined: 14-September 07

Re: temp converison

Posted 18 November 2009 - 07:04 PM

Put your question in the body of the post please.
Was This Post Helpful? 0
  • +
  • -

#3 vishalpiratla  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 18-November 09

Re: temp converison

Posted 18 November 2009 - 07:07 PM

View PostKYA, on 18 Nov, 2009 - 06:04 PM, said:

Put your question in the body of the post please.

sorry im still getting used to this site. but when I try to run this code i keep getting this error: Exception in thread "main" java.lang.Error: Unresolved compilation problems:
system cannot be resolved
Type mismatch: cannot convert from double to int

at tempconverter.main(tempconverter.java:14)
and i have no clue how to resolve it
Was This Post Helpful? 0
  • +
  • -

#4 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8328
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: temp converison

Posted 18 November 2009 - 08:06 PM

CelsiusTemp = farenheit * CONVERSION_FACTOR + Base;

CelsiusTemp is an int
CONVERSION_FACTOR a double

In Java if the left side of a = contains one double variable all operations are "upgraded to double

you cannot put a double into a int

let say:

int x;
double y = 1.0E100;

x = y; // you cannot store 1000000000000000000000...00000 a hundred times into a int

now if you know for sure that your double is convertibele to a int (behing in the int range) you can always cast

CelsiusTemp = (int) farenheit * CONVERSION_FACTOR + Base;
Was This Post Helpful? 0
  • +
  • -

#5 vishalpiratla  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 18-November 09

Re: temp converison

Posted 19 November 2009 - 01:54 PM

View Postpbl, on 18 Nov, 2009 - 07:06 PM, said:

CelsiusTemp = farenheit * CONVERSION_FACTOR + Base;

CelsiusTemp is an int
CONVERSION_FACTOR a double

In Java if the left side of a = contains one double variable all operations are "upgraded to double

you cannot put a double into a int

let say:

int x;
double y = 1.0E100;

x = y; // you cannot store 1000000000000000000000...00000 a hundred times into a int

now if you know for sure that your double is convertibele to a int (behing in the int range) you can always cast

CelsiusTemp = (int) farenheit * CONVERSION_FACTOR + Base;



View Postvishalpiratla, on 19 Nov, 2009 - 12:49 PM, said:

View Postpbl, on 18 Nov, 2009 - 07:06 PM, said:

CelsiusTemp = farenheit * CONVERSION_FACTOR + Base;

CelsiusTemp is an int
CONVERSION_FACTOR a double

In Java if the left side of a = contains one double variable all operations are "upgraded to double

you cannot put a double into a int

let say:

int x;
double y = 1.0E100;

x = y; // you cannot store 1000000000000000000000...00000 a hundred times into a int

now if you know for sure that your double is convertibele to a int (behing in the int range) you can always cast

CelsiusTemp = (int) farenheit * CONVERSION_FACTOR + Base;


thanks man that was really helpful. now I am encountering another problem. My program runs but its not giving me the correct answer. for example when i enter 32 degrees Fahrenheit i get a response of -14 degrees Celsius. i fiddled around with the code to make it work but i couldnt figure it out. here is the updated code, see if anyone can figure out where i am going wrong.

import java.util.Scanner; 

public class tempconverter {


	
	 
	public static void main(String[] args) {
		final int Base = -32;  
		final double CONVERSION_FACTOR = 5.0 / 9.0;
		int farenheit;
		double CelsiusTemp;
		
		Scanner scan = new Scanner (System.in);		
		System.out.print("Enter the farenheit temperature: ");
		farenheit = scan.nextInt();
		
		CelsiusTemp = (int)farenheit * CONVERSION_FACTOR + Base; 
		
		System.out.println("Celsius Equivalent: " + CelsiusTemp);
	}

}


Was This Post Helpful? 0
  • +
  • -

#6 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8328
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: temp converison

Posted 19 November 2009 - 06:05 PM

This formula is wrong

farenheit * CONVERSION_FACTOR + Base

the Base should be multiply by 5
the original formula is

C / 5 = (F - 32) / 9
so
C = (F - 32) / 9 / 5

the Base should be subtract before the CONVERSION_FACTOR is applied
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1