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 9 of 9

Thread: Temperature Calculation Code Java

  1. #1
    Junior Member
    Join Date
    Jan 2014
    Posts
    13
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Unhappy Temperature Calculation Code Java

    Hello, I am trying to create a java file that will print out the temperature in Fahrenheit. This is my code.
    import java.util.Scanner;
     
    public class TemperatureConverter 
    {
     
    	public static void main(String[] args) 
    	{
     
    		float temperature;
    		Scanner in= new Scanner(System.in);
     
     
    		System.out.printf("Enter temperature in degrees Celsius:  ");
    		temperature = in.nextInt();
     
                    //Getting a red X next to this line.
                   fahrenheit = temperature * 9/5 +32;
    		//Add this line
    		System.out.printf(+temperature + " degrees Celsius is " + fahrenheit + " degrees Fahrenheit.");
     
    		//System.out.println(temp1 + " degrees celsius is " + fahrenheit + " degrees Fahrenheit.");
    	}
     
    }
    I need to know what I am doing wrong. Using Eclipse as my platform. I have entered comments where I am getting red X's.

    The error I get when I run the program.

    Exception in thread "main" java.lang.Error: Unresolved compilation problems:
    fahrenheit cannot be resolved to a variable
    fahrenheit cannot be resolved to a variable

    at TemperatureConverter.main(TemperatureConverter.jav a:24)


  2. #2
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    4,778
    My Mood
    Mellow
    Thanks
    175
    Thanked 625 Times in 612 Posts

    Default Re: Temperature Calculation Code Java

    Welcome to the forum! Please read this topic to learn how to post code in code or highlight tags and other useful info for newcomers. Highlighting your code is not necessary and won't work when in code/highlight tags. Posting the complete error is enough.

  3. #3
    Junior Member
    Join Date
    Jan 2014
    Posts
    13
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Temperature Calculation Code Java

    Thank you for your direction.

  4. #4
    Member andbin's Avatar
    Join Date
    Dec 2013
    Location
    Italy
    Posts
    443
    Thanks
    4
    Thanked 122 Times in 114 Posts

    Default Re: Temperature Calculation Code Java

    Quote Originally Posted by lynn66 View Post
    fahrenheit cannot be resolved to a variable
    Isn't this clear? You have to declare a variable for fahrenheit.
    Andrea, www.andbin.net SCJP 5 (91%) SCWCD 5 (94%)

    Useful links for Java beginners My new project Java Examples on Google Code

  5. #5
    Junior Member
    Join Date
    Jan 2014
    Posts
    22
    My Mood
    Happy
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Temperature Calculation Code Java

    You need to declare Fahrenheit as a variable. (ie float, int, long, byte...etc)

  6. #6

    Default Re: Temperature Calculation Code Java

    First of all you have to declare Fahrenheit as a variable i.e float, int etc

  7. #7
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    4,778
    My Mood
    Mellow
    Thanks
    175
    Thanked 625 Times in 612 Posts

    Default Re: Temperature Calculation Code Java

    ECHO . . . echo . . . echo

  8. #8
    Junior Member
    Join Date
    Jan 2014
    Posts
    13
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Temperature Calculation Code Java

    Just wanted to let you know I solved the problem. Thank you for your help.

  9. #9
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    4,778
    My Mood
    Mellow
    Thanks
    175
    Thanked 625 Times in 612 Posts

    Default Re: Temperature Calculation Code Java

    Thanks for letting us know.

Similar Threads

  1. simple java calculation
    By dulitul in forum What's Wrong With My Code?
    Replies: 5
    Last Post: October 12th, 2013, 11:23 AM
  2. simple calculation code
    By dokunbam in forum What's Wrong With My Code?
    Replies: 7
    Last Post: October 8th, 2013, 05:55 AM
  3. [SOLVED] Can anyone explain this code to me? Calculation of mean and variance.
    By hemla in forum Object Oriented Programming
    Replies: 5
    Last Post: March 24th, 2013, 04:17 PM
  4. Simple Java Code Help--- temperature conversion using while loop
    By Idiot_willing_to_learn in forum What's Wrong With My Code?
    Replies: 3
    Last Post: February 26th, 2013, 10:33 AM
  5. loan calculation applet code
    By haxn in forum AWT / Java Swing
    Replies: 1
    Last Post: September 1st, 2012, 12:38 AM

Tags for this Thread