# Problem converting F to C vise versa

• (2 Pages)
• 1
• 2

## 24 Replies - 2988 Views - Last Post: 02 October 2012 - 03:04 AMRate 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=293741&amp;s=913802f71de2de813da6c2492de3595f&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #16 rbk3ai

Reputation: -4
• Posts: 64
• Joined: 08-September 12

## Re: Problem converting F to C vise versa

Posted 30 September 2012 - 02:54 PM

Sorry Im a newbie I just started learning this 5 days ago So idk wth im doing bare with me guys im sorry for my noobness i will get to your status one day. I'm having trouble with the println

```import java.util.Scanner;

public class Temp
{
static double celsius;
static double fahrenheit;

public static void main (String[] args)
{

System.out.print("Please enter the temperature in degrees: ");
Scanner in = new Scanner(System.in);
double temp = in.nextDouble();
System.out.print("Enter F for Farhenheit or C for Celsius: ");
String type = in.next();

if(type.equalsIgnoreCase("c")){
celsius = (5.0/9.0) * fahrenheit - 32.0;
System.out.println(temp + " Farhenheit = " + celsius + " Celsius.");
}
if(type.equalsIgnoreCase("f")){
fahrenheit = (9.0/5.0) * celsius + 32.0;
System.out.println(temp + " Celsius = " + fahrenheit + " Farhenheit.");
}

}
}

```

This post has been edited by rbk3ai: 30 September 2012 - 02:55 PM

### #17 GregBrannon

• D.I.C Lover

Reputation: 2250
• Posts: 5,340
• Joined: 10-September 10

## Re: Problem converting F to C vise versa

Posted 30 September 2012 - 02:58 PM

C'mon, what trouble? We shouldn't have to guess what your questions are.

### #18 rbk3ai

Reputation: -4
• Posts: 64
• Joined: 08-September 12

## Re: Problem converting F to C vise versa

Posted 30 September 2012 - 03:07 PM

My problem is that the print comes out wrong say i enter 65 F it brings -32 C which is way off!

Edit: I'm sorry my question is how to fix it so that it prints out right... right now it prints 65F into -32C. where did i go wrong so i know how to fix it.

This post has been edited by rbk3ai: 30 September 2012 - 03:08 PM

### #19 GregBrannon

• D.I.C Lover

Reputation: 2250
• Posts: 5,340
• Joined: 10-September 10

## Re: Problem converting F to C vise versa

Posted 30 September 2012 - 03:14 PM

Edit: The problem is not with the printing but with the logic before the printing.

Recheck your formulas to see what, if anything, they do with the number entered by the user.

This post has been edited by GregBrannon: 30 September 2012 - 03:25 PM

### #20 rbk3ai

Reputation: -4
• Posts: 64
• Joined: 08-September 12

## Re: Problem converting F to C vise versa

Posted 30 September 2012 - 04:41 PM

Ok i completed the code to what i thought was right, it gives the right numbers now but the online corrector says while its close to correct i have an error i dont see it?

```import java.util.Scanner;

public class Temp
{
static double celsius;
static double fahrenheit;

public static void main (String[] args)
{

System.out.print("Please enter the temperature in degrees: ");
Scanner in = new Scanner(System.in);
double temp = in.nextDouble();
System.out.print("Enter F for Farhenheit or C for Celsius: ");
String type = in.next();

if(type.equalsIgnoreCase("c")){
celsius = (5.0/9.0)*(temp - 32.0);
System.out.println(temp + " Farhenheit = " + celsius + " Celsius.");
}
if(type.equalsIgnoreCase("f")){
fahrenheit = (9.0/5.0)*temp + 32;
System.out.println(temp + " Fahrenheit = " + fahrenheit + " Celsius.");
}

}
}

```

here is the error log

```Input test1.in
68.0 F
fail; Actual: 68.0
Expected: 20.0
fail; Actual: 154.4
Expected:
Input test2.in
10.0 C
fail; Actual: 10.0
Expected: 50.0
fail; Actual: -12.222222222222223
Expected:
Input test3.in
-10.0 C
fail; Actual: -10.0
Expected: 14.0
fail; Actual: -23.333333333333336
Expected:
Input test4.in
32.0 F
fail; Actual: 32.0
Expected: 0.0
fail; Actual: 89.6
Expected:
Comparison failed.

```

### #21 GregBrannon

• D.I.C Lover

Reputation: 2250
• Posts: 5,340
• Joined: 10-September 10

## Re: Problem converting F to C vise versa

Posted 30 September 2012 - 04:53 PM

Two easy checks of an F to C converter are 32F should be 0C, 212F should be 100C, and vice-versa for both. Those aren't working in your program.

What happens if you swap the "f" and "c"?

These are things you should try before asking for help.

I also notice you have 32.0 in one place and 32 in another. While I don't think it makes a difference to the result, consistency is a good thing.

This post has been edited by GregBrannon: 30 September 2012 - 04:53 PM

### #22 Sergio Tapia

• D.I.C Lover

Reputation: 1258
• Posts: 4,168
• Joined: 27-January 10

## Re: Problem converting F to C vise versa

Posted 30 September 2012 - 07:00 PM

rbk3ai, on 30 September 2012 - 05:02 PM, said:

GregBrannon, on 30 September 2012 - 02:00 PM, said:

Please post the latest version of the code and describe the latest problems, errors, questions, etc. It's very hard to determine the current status from the latest post when one has to go back to the first post and add in all of the snippets posted since.

Relax, take a breath, it'll be okay.

I haven't got much greg, but this is where I'm at. Also, whats the best way to teach this stuff my teacher gave us a book from 2006 and Im lost i want to learn on my own i dont like the teaching system.

```import java.util.Scanner;

public class Temp
{
public static void main (String[] args)
{
final double celsius;
final double fahrenheit;
System.out.print("Please enter the temperature in degrees: ");
Scanner in = new Scanner(System.in);
double temp = in.nextDouble();
System.out.print("Enter F for Farhenheit or C for Celsius: ");
String type = in.next();

if(type.equalsIgnoreCase("f"))
fahrenheit = (9/5) * celsius + 32.0;

if(type.equalsIgnoreCase("c"))
celsius = (5/9) * fahrenheit - 32.0;
}
}

```

### #23 rbk3ai

Reputation: -4
• Posts: 64
• Joined: 08-September 12

## Re: Problem converting F to C vise versa

Posted 30 September 2012 - 07:14 PM

This is where I'm at in my compiler it looks fine it comes out funny on here sorry...

```01	import java.util.Scanner;
02
03	public class Temp
04	{
05	      static double celsius;
06	      static double fahrenheit;
07
08	       public static void main (String[] args)
09	               {
10
11	 System.out.print("Please enter the temperature in degrees: ");
12	 Scanner in = new Scanner(System.in);
13	      double temp = in.nextDouble();
14	System.out.print("Enter F for Farhenheit or C for Celsius: ");
15	String type = in.next();
16
17	            if(type.equalsIgnoreCase("f"));{
18	             celsius = (5.0/9.0)*(temp - 32.0);
19	            System.out.println(temp + "F" + " Farhenheit = " + celsius + " Celsius.");
20	              }
21
22	         if(type.equalsIgnoreCase("c")){
23	            fahrenheit = (9.0/5.0)*temp + 32.0;
24	               System.out.println(temp  + "C" + " Celsius = " + fahrenheit + " Farhenheit.");
25	                      }
26
27
28
29
30
31	}
32	}

```

This post has been edited by rbk3ai: 30 September 2012 - 07:15 PM

### #24 nick2price

• D.I.C Lover

Reputation: 565
• Posts: 2,826
• Joined: 23-November 07

## Re: Problem converting F to C vise versa

Posted 02 October 2012 - 02:13 AM

The formula should be

Quote

°C x 9/5 + 32 = °F

(°F - 32) x 5/9 = °C

This means
```celsius = (temp - 32.0)*5.0/9.0;

fahrenheit = (temp * 9)/5 + 32.0;
```

You have to remember the bodmas rule, I think the above should be ok although I am not the best at maths.

### #25 GregBrannon

• D.I.C Lover

Reputation: 2250
• Posts: 5,340
• Joined: 10-September 10

## Re: Problem converting F to C vise versa

Posted 02 October 2012 - 03:04 AM

BODMAS rule.

I'd never heard that before, but I was one to whom math came easy.

• (2 Pages)
• 1
• 2

 .related ul { list-style-type: circle; font-size: 12px; font-weight: bold; } .related li { margin-bottom: 5px; background-position: left 7px !important; margin-left: -35px; } .related h2 { font-size: 18px; font-weight: bold; } .related a { color: blue; }