# Temperature Conversion

Page 1 of 1

## 8 Replies - 1450 Views - Last Post: 11 February 2011 - 03:14 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=215013&amp;s=01560095d16f5bfd6caa09b09d72316b&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 shortman12012

Reputation: 0
• 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

• Games, Graphs, and Auctions

Reputation: 12267
• Posts: 45,362
• 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.

### #3 shortman12012

Reputation: 0
• 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

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?

### #4 macosxnerd101

• Games, Graphs, and Auctions

Reputation: 12267
• Posts: 45,362
• Joined: 27-December 08

## Re: Temperature Conversion

Posted 09 February 2011 - 09:53 AM

What are you inputting? Please post exactly.

### #5 shortman12012

Reputation: 0
• 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

### #6 macosxnerd101

• Games, Graphs, and Auctions

Reputation: 12267
• Posts: 45,362
• 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.

### #7 shortman12012

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

## Re: Temperature Conversion

Posted 09 February 2011 - 10:01 AM

alright thank you so much! it finally worked

• Lux Ex Tenebris

Reputation: 14
• 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

• Lux Ex Tenebris

Reputation: 14
• 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