Temperature converter

converts fahrenheit to celsius and celsius to fahrenheit

Page 1 of 1

9 Replies - 37126 Views - Last Post: 06 July 2012 - 03:16 PM Rate Topic: -----

#1 gmillerlight  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 27
  • Joined: 30-April 09

Temperature converter

Post icon  Posted 07 June 2009 - 07:53 PM

not asking user for temp to convert just printing print line statements.
package celsiusfahrenheitconverter;
import java.util.Scanner;

/** This java code reads
 *  Fahrenheit temperature the
 * user enters and displays back
 * temperature in Celsius. This also
 * Reads the Celsius temperature
 * entered and displays back
 * Fahrenheit temperature.
 *
 * @author 
 */
public class CelsiusFahrenheitConverter
{

	/**
	 * @param args the command line arguments
	 */
	public static void main(String[] args)
	{
		  // declare variables
		String from;
		String to;
		String temp;
		int choice;
		int fahrenheit;
		int celsius;

		Scanner input = new Scanner(System.in);
	   
		do
		{
			choice = -1;
			System.out.println("Convert tmperature:");
			System.out.println(" Convert from Fahrenheit to Celsius:");
			System.out.println(" Convert from Celsius to Fahrenheit:");
			System.out.println("Enter 3 - Exit:");
			choice = input.nextInt();

		switch (choice)
		{
			// case 1 asks user for a Fahrenheit temp to convert to Celsius
			// case 2 asks user for a Celsius temp to convert to Fahrenheit
		   case 1:
				 
				System.out.println("Enter the temperature in Fahrenheit:");
				fahrenheit = input.nextInt();
				celsius = convertToCelsius(fahrenheit);
				from = "Fahrenheit";
				to = "Celsius";
				break;

		   case 2:
				 
				System.out.println("Enter the temperature in Celsius:");
				celsius = input.nextInt();
				fahrenheit = convertToFahrenheit(celsius);
				from = "Celsius";
				to = "Fahrenheit";
				break;
			  default:
					continue; // no input or output
		}

				 System.out.print("\n");
				 System.out.print(fahrenheit);
			  
				 System.out.print(from);
				 System.out.print("=");
				 System.out.print(celsius);
			   
				 System.out.print(to);
				 System.out.print('\n');
		}
			while (choice != 3);
	}

			/**
	 * Converts form Celsius to
	 * Fahrenheit
	 * @param temp The temperature
	 * in Celsius
	 * @return The temperature in
	 * Fahrenheit
	 */

	public static int convertToFahrenheit(int temp)
	{
		return (9 * temp)/ 5 + 32;
	}

	/**
	 * Converts from Fahrenheit
	 * to Celsius
	 * @param temp the temperature
	 * in Fahrenheit
	 * @return the temperature in Celsius
	 */

	 public static int convertToCelsius(int temp)
	 {
		 return (temp - 32) / 9 * 5;
	 }

}

/*run:
Convert tmperature:
 Convert from Fahrenheit to Celsius:
 Convert from Celsius to Fahrenheit:
Enter 3 - Exit:*/

This post has been edited by gmillerlight: 07 June 2009 - 07:54 PM


Is This A Good Question/Topic? 0
  • +

Replies To: Temperature converter

#2 Martyr2  Icon User is offline

  • Programming Theoretician
  • member icon

Reputation: 4337
  • View blog
  • Posts: 12,137
  • Joined: 18-April 07

Re: Temperature converter

Posted 07 June 2009 - 09:25 PM

I am not sure what you are asking but I believe both of your equations for conversion are wrong. You also have to take into account that 5/9 won't return .55555 etc, it will return 0 because you are doing integer division. Make sure you convert them to doubles and use doubles throughout your equation. If you want it to return int, cast it to an int at the end.

return (int)(((9.0 / 5.0) * (double)temp) + 32.0);

and 

return (int)((5.0 / 9.0) * ((double)temp - 32.0));



Lastly you will need to take a look at the printing at the end and watch which labels you are printing because they are backwards. Don't switch the "to" and "from" variable labels because you are then not switching what variables you print.

Good luck.
Was This Post Helpful? 0
  • +
  • -

#3 gmillerlight  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 27
  • Joined: 30-April 09

Re: Temperature converter

Posted 08 June 2009 - 05:39 AM

Quote

I have made the changes that were suggested, but it is still not asking me for a temperature to convert. However the enter 3 to exit works just fine. I included the Run output so you can see what I mean. I did enter 59 but it didn't do anything.


package celsiusfahrenheitconverter;
import java.util.Scanner;

/** This java code reads
 *  Fahrenheit temperature the
 * user enters and displays back
 * temperature in Celsius. This also
 * Reads the Celsius temperature
 * entered and displays back
 * Fahrenheit temperature.
 *
 * @author 
 */
public class CelsiusFahrenheitConverter
{

	/**
	 * @param args the command line arguments
	 */
	public static void main(String[] args)
	{
		  // declare variables
		String from;
		String to;
		String temp;
		int choice;
		int fahrenheit;
		int celsius;

		Scanner input = new Scanner(System.in);
	   
		do
		{
			choice = -1;
			System.out.println("Convert temperature:");
			System.out.println(" Convert from Fahrenheit to Celsius:");
			System.out.println(" Convert from Celsius to Fahrenheit:");
			System.out.println("Enter 3 - Exit:");
			choice = input.nextInt();

		switch (choice)
		{
			// case 1 asks user for a Fahrenheit temp to convert to Celsius
			// case 2 asks user for a Celsius temp to convert to Fahrenheit
		   case 1:
				 
				System.out.println("Enter the temperature in Fahrenheit:");
				fahrenheit = input.nextInt();
				celsius = convertToCelsius(fahrenheit);
				from = "Celsius";
				to = "Fahrenheit";
				break;

		   case 2:
				 
				System.out.println("Enter the temperature in Celsius:");
				celsius = input.nextInt();
				fahrenheit = convertToFahrenheit(celsius);
				from = "Fahrenheit";
				to = "Celsius";
				break;
			  default:
					continue; // no input or output
		}

				 System.out.print("\n");
				 System.out.print(fahrenheit);
			  
				 System.out.print(from);
				 System.out.print("=");
				 System.out.print(celsius);
			   
				 System.out.print(to);
				 System.out.print('\n');
		}
			while (choice != 3);
	}

			/**
	 * Converts form Celsius to
	 * Fahrenheit
	 * @param temp The temperature
	 * in Celsius
	 * @return The temperature in
	 * Fahrenheit
	 */

	public static int convertToFahrenheit(int temp)
	{
		return (int)(((9.0 / 5.0) * (double)temp) + 32.0);


	}

	/**
	 * Converts from Fahrenheit
	 * to Celsius
	 * @param temp the temperature
	 * in Fahrenheit
	 * @return the temperature in Celsius
	 */

	 public static int convertToCelsius(int temp)
	 {
		 return (int)((5.0 / 9.0) * ((double)temp - 32.0));

	 }

}
/*run:
Convert tmperature:
 Convert from Fahrenheit to Celsius:
 Convert from Celsius to Fahrenheit:
Enter 3 - Exit:
59
Convert tmperature:
 Convert from Fahrenheit to Celsius:
 Convert from Celsius to Fahrenheit:
Enter 3 - Exit:
59
Convert tmperature:
 Convert from Fahrenheit to Celsius:
 Convert from Celsius to Fahrenheit:
Enter 3 - Exit:
3
BUILD SUCCESSFUL (total time: 1 minute 12 seconds)
*/


	

This post has been edited by gmillerlight: 08 June 2009 - 05:42 AM

Was This Post Helpful? 0
  • +
  • -

#4 computerfox  Icon User is offline

  • straight vegetarian kid

Reputation: 50
  • View blog
  • Posts: 3,772
  • Joined: 29-January 09

Re: Temperature converter

Posted 08 June 2009 - 06:29 AM

i'm guessing it's okay now :unsure:
Was This Post Helpful? 0
  • +
  • -

#5 gmillerlight  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 27
  • Joined: 30-April 09

Re: Temperature converter

Posted 08 June 2009 - 06:45 AM

Quote

No this is not working correctly yet. The exit is working but it is not doing anything the temperature to convert to either celsius or fahrenheit. Please help I'm a beginner and really not sure what I did wrong.

Was This Post Helpful? 0
  • +
  • -

#6 computerfox  Icon User is offline

  • straight vegetarian kid

Reputation: 50
  • View blog
  • Posts: 3,772
  • Joined: 29-January 09

Re: Temperature converter

Posted 08 June 2009 - 06:51 AM

okay i was gonna do this earlier, but i wasn't sure of something. click here now it's in C++, but it's set up relatively easy to convert. just make a new variable and then return the new variable

for example if you change the cout<< to return then it would work. hope that helps :)


PLEASE READ THE INSTRUCTION IN THIS POST CAREFULLY

This post has been edited by computerfox: 08 June 2009 - 08:55 AM

Was This Post Helpful? 1
  • +
  • -

#7 computerfox  Icon User is offline

  • straight vegetarian kid

Reputation: 50
  • View blog
  • Posts: 3,772
  • Joined: 29-January 09

Re: Temperature converter

Posted 08 June 2009 - 09:11 AM

*facepalm* i'll help you out dude...


public double convertTemp(double temp,char from,char to)
{
		  from=from.toUpperCase();
		  to=to.toUpperCase();
		  int answer1=from.compareTo("C");
		  int answer2=to.compareTo("F");

	  //from c to f
		  if(answer1==0&&answer2==0)
	  {
		  return (((temp/5)*9)+32);
	  }
	  
	  //from f to c
		  answer1=from.compareTo("F");
		  answer2=to.compareTo("C");
	  if(answer1==0&&answer2==0)
	  {

		   return (((temp-32)*5)/9);
	  }
	  
	
}



hope that helps :)
Was This Post Helpful? 1
  • +
  • -

#8 dwheeler0603  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 21
  • Joined: 03-November 11

Re: Temperature converter

Posted 05 July 2012 - 04:12 PM

I see what your trying to do but it seems like the method you are approaching this problem with is not the easiest, your making more work for yourself than you need.

you started things off right with importing scanner for user input, I would scrap the package you are trying to use though.
you started your class and main method so we are ready for business. After your main method declaration try using this code

Scanner keyboard = new Scanner(System.in);//allow user keyboard entry
		char reply;

		do {
			System.out.print(" Enter c to input a temperature in celsius or f for fahrenheit :");
			reply =
				keyboard.findWithinHorizon(".",0) .charAt(0) ;
		} while (reply != 'c' && reply != 'f');

This is basically what you tried but this allows the user to make a selection with one letter (char reply;) hopefully you understand what is happening in the rest of the code, if not just ask. now declare your variables, you want them to be double not int, make two variables name them what ever you would like, I would do cT(celsius) and fT(fahrenheit). Next you need two if loops, one that accounts for user selection c and one for f, remember it should look something like if (reply == 'c') {. Now you need to tell the user to input a temperature in celsius(for the selection of c) Now you need to define in your program the equation to convert celsius to fahrenheit, using my variables it would look like fT= cT * 9/5 + 32; From here on out you should now what needs to happen System.out.print to show results, a second if loop for fahrenheit to celsius and double check that all your brackets are in place as they should be. This method is different from how you have approached this and it should save you several lines of code, give it a shot and if you have any questions just ask. Hope this helps put you in the right direction
Was This Post Helpful? 0
  • +
  • -

#9 pbl  Icon User is offline

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

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

Re: Temperature converter

Posted 06 July 2012 - 12:24 PM

You are entering the temerature to convert to soon

Convert tmperature:
 Convert from Fahrenheit to Celsius:
 Convert from Celsius to Fahrenheit:
Enter 3 - Exit:
59                    <--- should be 1 or 2 here not 59
Convert tmperature:
 Convert from Fahrenheit to Celsius:
 Convert from Celsius to Fahrenheit:
Enter 3 - Exit:
59                    <--- should be 1 or 2 here not 59


Was This Post Helpful? 0
  • +
  • -

#10 SPorter  Icon User is offline

  • New D.I.C Head
  • member icon

Reputation: 11
  • View blog
  • Posts: 31
  • Joined: 29-June 12

Re: Temperature converter

Posted 06 July 2012 - 03:16 PM

I would also recommend not doing everything through a loop like you are with the do/while statement.

Instead, it would be better to do it once then add an option for the user to do another. Something like this:

public void promptUser(){
    //Prompt the user here
    ...
    //Call your calculation methods to calculate the conversion
    ...
    System.out.println("Would you like to do another conversion");
    if{user said yes)
        promptUser();
    else{
        System.out.print("Thank you");
        System.exit(0);
    }
}

Something like this would work better. This way the user doesn't have to see the menu every time they want to do another conversion from the first run of your program. Imagine if every time you turned on your TV or changed the channel, you had to see the TV menu options... that would get pretty annoying lol.


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1