Problem converting F to C vise versa

  • (2 Pages)
  • +
  • 1
  • 2

24 Replies - 2012 Views - Last Post: 02 October 2012 - 03:04 AM Rate Topic: -----

#1 rbk3ai  Icon User is offline

  • D.I.C Head

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

Problem converting F to C vise versa

Posted 30 September 2012 - 12:21 PM

Ok I cant figure out how to get this to work i know the formula to convert them but i cant seem to understand how to put them in java language any insight is appreciated. I have no idea what to do for the system.out.print neither. Because i cant even get the formula into java language

public class Temps
{
   public static void main (String[] args)
   {
      // Define constants
      
      final double celsius;
      final double fahrenheit;

      // Display prompt for temperature in degrees Farhenheit or Celsius
      System.out.print("Please enter the temperature in degrees: ");

      // Read temperature
      Scanner in = new Scanner(System.in);
      double temp = in.nextDouble();

      // Display prompt for character that denotes type of temperature
      System.out.print("Enter F for Farhenheit or C for Celsius: ");

      // Read character denoting type of temperature
      String type = in.next();

      // Compute and print Celsius or Farhenheit equivalent

      
  if(type.equalsIgnoreCase("f"))
	          fahrenheit = (9/5)celsius + 32);
	          System.out.println(temp + " Farhenheit = " + result + " Celsius.");

         if(type.equalsIgnoreCase("c"))
	          celsius = (5/9)(fahrenheit - 32.0)
	          System.out.println(temp + " Celsius = " + result + " Fahrenheit.");
			 
   }
}


This post has been edited by rbk3ai: 30 September 2012 - 12:26 PM


Is This A Good Question/Topic? 0
  • +

Replies To: Problem converting F to C vise versa

#2 natecat  Icon User is offline

  • D.I.C Head

Reputation: 53
  • View blog
  • Posts: 225
  • Joined: 19-December 11

Re: Problem converting F to C vise versa

Posted 30 September 2012 - 12:35 PM

First thing I noticed is that you have a extra parentheses hanging around on this line:
fahrenheit = (9/5)celsius + 32);

and on this line you forgot a semicolon:
celsius = (5/9)(fahrenheit - 32.0)


Also, on your scanner, you should read in.nextLine(); instead of in.next();
Was This Post Helpful? 1
  • +
  • -

#3 rbk3ai  Icon User is offline

  • D.I.C Head

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

Re: Problem converting F to C vise versa

Posted 30 September 2012 - 12:40 PM

View Postnatecat, on 30 September 2012 - 12:35 PM, said:

First thing I noticed is that you have a extra parentheses hanging around on this line:
fahrenheit = (9/5)celsius + 32);

and on this line you forgot a semicolon:
celsius = (5/9)(fahrenheit - 32.0)


Also, on your scanner, you should read in.nextLine(); instead of in.next();


Temp.java:16: ')' expected
	          fahrenheit = (9/5 celsius + 32);
	                           ^
Temp.java:16: not a statement
	          fahrenheit = (9/5 celsius + 32);



Temp.java:20: ';' expected
	          celsius = (5/9)(fahrenheit - 32.0);
	                         ^
Temp.java:20: not a statement
	          celsius = (5/9)(fahrenheit - 32.0);
                                 


This post has been edited by rbk3ai: 30 September 2012 - 12:47 PM

Was This Post Helpful? 0
  • +
  • -

#4 natecat  Icon User is offline

  • D.I.C Head

Reputation: 53
  • View blog
  • Posts: 225
  • Joined: 19-December 11

Re: Problem converting F to C vise versa

Posted 30 September 2012 - 12:42 PM

The ending one, not the middle one
Was This Post Helpful? 1
  • +
  • -

#5 rbk3ai  Icon User is offline

  • D.I.C Head

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

Re: Problem converting F to C vise versa

Posted 30 September 2012 - 12:50 PM

Temp.java:16: not a statement
	          fahrenheit = (9/5)celsius + 32;


Was This Post Helpful? 0
  • +
  • -

#6 rbk3ai  Icon User is offline

  • D.I.C Head

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

Re: Problem converting F to C vise versa

Posted 30 September 2012 - 01:03 PM

Any other help? I've tried what you suggested still doesn't help.
Was This Post Helpful? 0
  • +
  • -

#7 natecat  Icon User is offline

  • D.I.C Head

Reputation: 53
  • View blog
  • Posts: 225
  • Joined: 19-December 11

Re: Problem converting F to C vise versa

Posted 30 September 2012 - 01:33 PM

You need to have some sort of operation between the (9/5) and the celcius variable
Was This Post Helpful? 1
  • +
  • -

#8 rbk3ai  Icon User is offline

  • D.I.C Head

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

Re: Problem converting F to C vise versa

Posted 30 September 2012 - 01:39 PM

View Postnatecat, on 30 September 2012 - 01:33 PM, said:

You need to have some sort of operation between the (9/5) and the celcius variable


like this
Temp.java:20: variable celsius might not have been initialized
	          fahrenheit = (9/5) + celsius + 32;
	                                        ^
Temp.java:17: variable fahrenheit might not have been initialized
	          celsius = (5/9) + fahrenheit - 32.0;


This post has been edited by rbk3ai: 30 September 2012 - 01:46 PM

Was This Post Helpful? 0
  • +
  • -

#9 GregBrannon  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2205
  • View blog
  • Posts: 5,239
  • Joined: 10-September 10

Re: Problem converting F to C vise versa

Posted 30 September 2012 - 02:00 PM

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.
Was This Post Helpful? 2
  • +
  • -

#10 rbk3ai  Icon User is offline

  • D.I.C Head

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

Re: Problem converting F to C vise versa

Posted 30 September 2012 - 02:02 PM

View PostGregBrannon, 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;
}
}


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

Was This Post Helpful? 0
  • +
  • -

#11 Ryano121  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1363
  • View blog
  • Posts: 3,002
  • Joined: 30-January 11

Re: Problem converting F to C vise versa

Posted 30 September 2012 - 02:09 PM

In Java (5/9) will not give you the result you expect. It is integer division so will be rounded. Instead do (5.0 / 9.0) which is double arithmetic and will give you a decimal result.
Was This Post Helpful? 1
  • +
  • -

#12 rbk3ai  Icon User is offline

  • D.I.C Head

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

Re: Problem converting F to C vise versa

Posted 30 September 2012 - 02:15 PM

I get this how to fix it? I cant initialize it can i? because it will change if someone picks different numbers?

Temp.java:16: variable celsius might not have been initialized
	          fahrenheit = (9.0/5.0) * celsius + 32.0;
	                                   ^
Temp.java:19: variable fahrenheit might not have been initialized
	          celsius = (5.0/9.0) * fahrenheit - 32.0;


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

Was This Post Helpful? 0
  • +
  • -

#13 GregBrannon  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2205
  • View blog
  • Posts: 5,239
  • Joined: 10-September 10

Re: Problem converting F to C vise versa

Posted 30 September 2012 - 02:34 PM

You are repeating mistakes because you don't understand the use of variables and their many possible flavors.

Why are celsius and fahrenheit final? (And if final, how can you expect to declare them without initializing them?)

An alternative at your stage is to declare static or class variables, and, as pointed out before, modify your equations so that a decimal number will result from a division:
import java.util.Scanner;

public class TestClass3
{
    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("f"))
            fahrenheit = (9.0/5.0) * celsius + 32.0;

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

You still need to print out the answer.

These are pretty basic concepts that you've missed along the way. If you want to succeed in this course, you need to reprioritize this class in your study schedule and catch up. Ya, I know, the instructor is lacking, but use the resources that are available to you to make up for his/her shortcomings. It's your fate over which you have some responsibility and control, not the instructor's.
Was This Post Helpful? 1
  • +
  • -

#14 rbk3ai  Icon User is offline

  • D.I.C Head

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

Re: Problem converting F to C vise versa

Posted 30 September 2012 - 02:41 PM

Thanks!

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

Was This Post Helpful? 0
  • +
  • -

#15 GregBrannon  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2205
  • View blog
  • Posts: 5,239
  • Joined: 10-September 10

Re: Problem converting F to C vise versa

Posted 30 September 2012 - 02:47 PM

Quit posting snippets that make no sense without the context of the rest of the program. Look at what I posted, because it works without error. Figure out why what you've done is wrong based on my error-free example. But I, or anyone else, can't help you, because I/we don't know what your program looks like.

Edit: Okay, I guess you reworked what you'd done, and discovered the solution. Happy day!

This post has been edited by GregBrannon: 30 September 2012 - 02:48 PM

Was This Post Helpful? 1
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2