8 Replies - 932 Views - Last Post: 11 February 2011 - 03:14 PM Rate Topic: -----

#1 shortman12012  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 37
  • Joined: 09-February 11

Temperature Conversion

Posted 09 February 2011 - 09:02 AM

Problem is to create a program that converts the user input temperature from celsius to fahrenheit or vice versa.




 System.out.println("Enter a temperature");
System.out.println("Enter C or c for celsius");
System.out.println("And F or f for fahrenheit");

String degree = keyboard.next();
double InputDegree, Degrees_C, Degrees_F, FinalDegree;       
InputDegree = keyboard.nextDouble();

if (degree.equals("F")) {
       Degrees_C = (5.0/9.0) * (InputDegree - 32);
       Degrees_C = FinalDegree;
       System.out.println("The temperature in Celsius is" + FinalDegree); 
       }
       else
       System.out.println("Error");

if (degree.equals("C")) {
     Degrees_F = ((9.0*(InputDegree)/5.0)+32);
     Degrees_F = FinalDegree;
     System.out.println("The temperature in Fahrenheit is" + Degrees_F);
     }
     else 
         System.out.println("Error");

    
}
               
        
        
    }



my problem is, I'm getting an error message that says FinalDegree is not initialized. I'm not sure how to fix this and just need some help to figure out the problem.

Is This A Good Question/Topic? 0
  • +

Replies To: Temperature Conversion

#2 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10185
  • View blog
  • Posts: 37,602
  • Joined: 27-December 08

Re: Temperature Conversion

Posted 09 February 2011 - 09:06 AM

Here you declare FinalDegree:
double InputDegree, Degrees_C, Degrees_F, FinalDegree;



To initialize it, you need to assign it an initial value:
FinalDegree = 0;



Always good practice to initialize your variables to prevent problems down the road.
Was This Post Helpful? 0
  • +
  • -

#3 shortman12012  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 37
  • Joined: 09-February 11

Re: Temperature Conversion

Posted 09 February 2011 - 09:50 AM

thanks, I did that, but now when I run the program I get this error message

Exception in thread "main" java.util.InputMismatchException
at java.util.Scanner.throwFor(Scanner.java:840)
at java.util.Scanner.next(Scanner.java:1461)
at java.util.Scanner.nextDouble(Scanner.java:2387)
at SortIntegers.main(SortIntegers.java:53)
Java Result: 1

im not really sure whats wrong with it?
Was This Post Helpful? 0
  • +
  • -

#4 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10185
  • View blog
  • Posts: 37,602
  • Joined: 27-December 08

Re: Temperature Conversion

Posted 09 February 2011 - 09:53 AM

What are you inputting? Please post exactly.
Was This Post Helpful? 0
  • +
  • -

#5 shortman12012  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 37
  • Joined: 09-February 11

Re: Temperature Conversion

Posted 09 February 2011 - 09:54 AM

the user is suppose to input a degree and then the letter F or C

so i tried imputing 32 F as well as 32 C
and i receive that message
Was This Post Helpful? 0
  • +
  • -

#6 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10185
  • View blog
  • Posts: 37,602
  • Joined: 27-December 08

Re: Temperature Conversion

Posted 09 February 2011 - 09:57 AM

You have to input the degree first, then the number. Not the number then degree.
Was This Post Helpful? 0
  • +
  • -

#7 shortman12012  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 37
  • Joined: 09-February 11

Re: Temperature Conversion

Posted 09 February 2011 - 10:01 AM

alright thank you so much! it finally worked
Was This Post Helpful? 0
  • +
  • -

#8 badjava  Icon User is offline

  • Lux Ex Tenebris
  • member icon

Reputation: 14
  • View blog
  • Posts: 540
  • Joined: 30-October 08

Re: Temperature Conversion

Posted 09 February 2011 - 10:07 AM

too messy to allow to live, post erased due to possible insanity.

This post has been edited by badjava: 11 February 2011 - 03:18 PM

Was This Post Helpful? 0
  • +
  • -

#9 badjava  Icon User is offline

  • Lux Ex Tenebris
  • member icon

Reputation: 14
  • View blog
  • Posts: 540
  • Joined: 30-October 08

Re: Temperature Conversion

Posted 11 February 2011 - 03:14 PM

Here is an example of how I like to make good descriptions when requesting input and displaying output. I was explaining this in my previous post but was really out of it. If someone wants to delete my previous post to clean up the thread that would be great.

To keep things simple I didn't shift input to upper case and handle either upper or lower case input and not get errors. I notice that you ask for upper or lower case input but I don't see the case being shifted in your code. Have you tried putting in a small case c or f? I'm curious what results you get.

I also got nothing but errors when trying to accept input like you have it coded. Does your code actually work? I'll have to try what you have on my windows machine and see if it flies there, curious..

my example:
import java.util.*;

public class conversion {
	
public static void main (String[] args) {
		
	double FinalDegree=0;
	
	System.out.println("Enter a temperature type of C or F (case sensitive) and a degree on one line.");
	System.out.println("(enter C for celsius or  F for fahrenheit)");
	System.out.println();
	System.out.println();
	
	double d1 = 0;
	String n1;
	
	Scanner keyboard = new Scanner(System.in);
	n1 = keyboard.next();
	d1 = keyboard.nextDouble( );

	if (n1.equals("F")) {
	   FinalDegree = ((d1-32) * 5/9);
       System.out.println("You entered the temperature of Farenheit, " + d1 +  " degrees.");
       System.out.println("Converted to Celsius this equals, C " + FinalDegree + " degrees.");       
	}

       else {
    	  if (n1.equals("C")) {
    		  FinalDegree = ((9.0*(d1)/5.0)+32);
    	       System.out.println("You entered the temperature of Celsius, " + d1 +  " degrees.");
    	       System.out.println("Converted to Farenheit this equals, F " + FinalDegree + " degrees.");
    	  }
    	  else {
    		  System.out.println("Errors in input or conversions, please try again.");
    	  }
    		  
       }
    	   
   }
}

This post has been edited by badjava: 16 February 2011 - 01:00 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1