Java School Assignment? Project Due Tomorrow? Chat LIVE With A Programming Expert!

Welcome to Dream.In.Code
Become a Java Expert!

Join 306,817 Java Programmers for FREE! Get instant access to thousands of Java experts, tutorials, code snippets, and more! There are 1,685 people online right now. Registration is fast and FREE... Join Now!




Temperature converter

 

Temperature converter, converts fahrenheit to celsius and celsius to fahrenheit

gmillerlight

7 Jun, 2009 - 06:53 PM
Post #1

New D.I.C Head
*

Joined: 30 Apr, 2009
Posts: 26

not asking user for temp to convert just printing print line statements.
CODE
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: 7 Jun, 2009 - 06:54 PM

User is offlineProfile CardPM
+Quote Post


Martyr2

RE: Temperature Converter

7 Jun, 2009 - 08:25 PM
Post #2

Programming Theoretician
Group Icon

Joined: 18 Apr, 2007
Posts: 7,305



Thanked: 837 times
Expert In: C/C++, Java, VB, VB.NET, C#, PHP, Web Development, HTML & CSS, Javascript

My Contributions
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.

CODE

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.


User is online!Profile CardPM
+Quote Post

gmillerlight

RE: Temperature Converter

8 Jun, 2009 - 04:39 AM
Post #3

New D.I.C Head
*

Joined: 30 Apr, 2009
Posts: 26



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.


CODE
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: 8 Jun, 2009 - 04:42 AM
User is offlineProfile CardPM
+Quote Post

computerfox

RE: Temperature Converter

8 Jun, 2009 - 05:29 AM
Post #4

straight vegetarian kid
*****

Joined: 29 Jan, 2009
Posts: 3,772



Thanked: 48 times
Dream Kudos: 1700
My Contributions
i'm guessing it's okay now unsure.gif
User is offlineProfile CardPM
+Quote Post

gmillerlight

RE: Temperature Converter

8 Jun, 2009 - 05:45 AM
Post #5

New D.I.C Head
*

Joined: 30 Apr, 2009
Posts: 26

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.

User is offlineProfile CardPM
+Quote Post

computerfox

RE: Temperature Converter

8 Jun, 2009 - 05:51 AM
Post #6

straight vegetarian kid
*****

Joined: 29 Jan, 2009
Posts: 3,772



Thanked: 48 times
Dream Kudos: 1700
My Contributions
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 smile.gif


PLEASE READ THE INSTRUCTION IN THIS POST CAREFULLY

This post has been edited by computerfox: 8 Jun, 2009 - 07:55 AM
User is offlineProfile CardPM
+Quote Post

computerfox

RE: Temperature Converter

8 Jun, 2009 - 08:11 AM
Post #7

straight vegetarian kid
*****

Joined: 29 Jan, 2009
Posts: 3,772



Thanked: 48 times
Dream Kudos: 1700
My Contributions
*facepalm* i'll help you out dude...


CODE

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 smile.gif
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic

Time is now: 11/20/09 10:13PM

Live Java Help!

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter Fan Us On Facebook

Java Tutorials

Reference Sheets

Java Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month