Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: Converting temperature program

  1. #1
    Junior Member
    Join Date
    Oct 2009
    Posts
    21
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Converting temperature program

    Alright so I'm having trouble creating this program, basically I need to prompt the user to enter a 1 or 2 if 1, you run a convert from Fahrenheit to celsius, or if the user presses 2, the program runs celsius to fahrenheit. Here's what I have so far:

    import java.util.Scanner;
     
    public class TemperatureConverter{
       public static void main(String[] args){
          Scanner console = new Scanner(System.in);
     
          System.out.println("Please press 1 to convert Fahrenheit to Celsius, or 2 to convert Celsius to Fahrenheit");
          int choice = console.nextInt();
                    if (choice == 1){
    	  	   ftoc();	  
    	  	 }
    	  	 else if (choice == 2){
    	  	   ctof();
    	  	  }
     
          System.out.println("What is the temperature?");
          double temperature = console.nextDouble();
     
     
          System.out.println("Your converted temperature is ");
       }
     
       //converts fahrenheit to celsius
       public static double ftoc (double degreesF){
          double degreesC = 5.0/9.0 * (degreesF - 32);
          return degreesC;
       }
     
       //converts celsius to fahrenheit
       public static double ctof (double degreesC){
          double degreesF = 9.0/5.0 * (degreesC + 32);
          return degreesF;
       }
     
    }

    I'm having trouble with what I need to do after the user puts a 1 or a 2 idk how I should go about if the user presses 1 then run this method, but if the user presses 2 run this method. It keeps giving me a bunch of errors saying that it's a variable, but I'm not assigning a variable, I'm calling the method to run that. I'm so confused


  2. #2
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,895
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: Converting temperature program

    your ftoc and ctof methods require you to pass a double as a parameter.

    Something like this:
    ftoc(5.0);

    You still need to get a number to convert from the user (use the nextDouble() method in Scanner).

  3. #3
    Junior Member
    Join Date
    Oct 2009
    Posts
    21
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Converting temperature program

    Huh? Do you mean instead of nextInt make it nextDouble? What do I put inside the parenthesis then?

  4. #4
    Senile Half-Wit Freaky Chris's Avatar
    Join Date
    Mar 2009
    Posts
    834
    My Mood
    Cynical
    Thanks
    7
    Thanked 105 Times in 90 Posts

    Default Re: Converting temperature program

    try this, just a re-arrange and helloworlds edit.

    import java.util.Scanner;
     
    public class TemperatureConverter{
       public static void main(String[] args){
          Scanner console = new Scanner(System.in);
     
          System.out.println("Please press 1 to convert Fahrenheit to Celsius, or 2 to convert Celsius to Fahrenheit");
          int choice = console.nextInt();
     
     
          System.out.println("What is the temperature?");
          double temperature = console.nextDouble();
          double t = 0.0;
          if (choice == 1){
    	  	  t = ftoc(temperature);	  
          }
          else if (choice == 2){
    	  	  t = ctof(temperature);
          }
     
          System.out.println("Your converted temperature is " + t );
       }
     
       //converts fahrenheit to celsius
       public static double ftoc (double degreesF){
          double degreesC = 5.0/9.0 * (degreesF - 32);
          return degreesC;
       }
     
       //converts celsius to fahrenheit
       public static double ctof (double degreesC){
          double degreesF = 9.0/5.0 * (degreesC + 32);
          return degreesF;
       }
     
    }

Similar Threads

  1. How to convert GSM 6.10 wav file to MP3/PCM/OGG/AU format using JAVA?
    By expertise in forum Java ME (Mobile Edition)
    Replies: 4
    Last Post: April 11th, 2014, 02:13 AM
  2. Converting a method from ArrayList so it is capable with an Array
    By BlueJ1 in forum Collections and Generics
    Replies: 2
    Last Post: July 8th, 2009, 05:22 PM
  3. [SOLVED] Java program to convert and compare integers
    By luke in forum What's Wrong With My Code?
    Replies: 9
    Last Post: May 18th, 2009, 06:26 PM
  4. Replies: 4
    Last Post: May 1st, 2009, 03:32 PM
  5. Program to convert Hexadecimal to its Character equivalent
    By nathanernest in forum Java Theory & Questions
    Replies: 2
    Last Post: April 8th, 2009, 03:12 AM