Solved

Temperature Converter Program

Posted on 2007-11-30
6
1,776 Views
Last Modified: 2013-11-23
Hi,
I'm just looking for some help in getting this code to compile. Any suggestions would be much appreciated.
Regards,
Niall
class TemperatureConverter 
{
	public static void main(String[] args) 
	{
		System.out.print("Enter temperature type. Fahrenheit [F/f], Celsius [C/c] :   ");
		String temp_unit = TextIO.getlnString();
		System.out.print("Enter the temperature: ");
		Double temp_value = TextIO.getlnDouble();
		
		if(		temp_unit.equals("f")	||	temp_unit.equals("F")	)
			System.out.println("Fahrenheit mapped to Celsius = " + covertCtoF( temp_value ) );
		else if(		temp_unit.equals("c")	||	temp_unit.equals("C")	)
			System.out.println("Fahrenheit mapped to Celsius = " + covertFtoC( temp_value ) );
		else	
			System.out.println("Enter only C/c or F/f, please try again.");
			break;
		
	}
	
	static double covertCtoF(Double temp_C)
	{
		return ( ( temp_C / 5 ) * 9 ) + 32;
	}
 
	static double covertFtoC(Double temp_F)
	{
		return ( ( temp_F - 32 )  / 9 ) * 5;
	}
}

Open in new window

0
Comment
Question by:niallmunroe
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
6 Comments
 
LVL 3

Accepted Solution

by:
CPlusJavaCSharp earned 150 total points
ID: 20386897
What is TextIO- looks like a user defined class for input- should you import it...
What about Scanner class for input..

You also repeat Fahrenheit mapped to Cel.. one should be Celsuis mapped to Fahern...?

0
 

Author Comment

by:niallmunroe
ID: 20390195
Many thanks for your reply. Done some more work & it compiles and runs ok now.
TextIO is user defined but compiled in the directory where stored so no need to import.
Well spotted F mapped to C twice.

Just one more small issue-How do I format the output to two decimal places?

if(temp_unit.equals("f")	||	temp_unit.equals("F")	)
System.out.printf("The temperature "+temp_value+" degrees Celsius is "+covertCtoF(temp_value)+" degrees Fahrenheit");

Open in new window

0
 
LVL 11

Assisted Solution

by:spoxox
spoxox earned 150 total points
ID: 20393492
Info on formatting the output can be found here:

http://java.sun.com/docs/books/tutorial/java/data/numberformat.html

0
 
LVL 1

Expert Comment

by:kunalthakur
ID: 21384421
here is an example....
System.out.format("%.2f%n", tempvalue);
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
tomcat startup error 5 136
java example issue 5 43
running on tomcat not jboss eap 7.0 3 30
Java array 21 82
INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:

749 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question