Results 1 to 20 of 20
Like Tree1Likes
  • 1 Post By lenois

Thread: New to forums (and need help)

  1. #1
    smoovejayy is offline Jonathan
    Join Date
    Feb 2013
    Location
    Bronx, NY
    Posts
    10
    Rep Power
    0

    Default New to forums (and need help)

    Hi everyone! I hope I can find some valuable friends here in these forums, as well as some help with java-related questions ;)
    I like to program and can see myself doing this as a career. I would love to know EVERYTHING there is to know about Java :)

    Anyway, I have code that doesn't seem to display the correct results. In the table, under Celsius, is supposed to show Fahrenheit degrees converted into Celsius, and all that keeps showing is -0.0 ~_~ I'm getting so frustrated lol. I'm playing catchup in my java course, trying to re-learn all this crap, and can't seem to understand my error(s) here.

    Any help is greatly appreciated :)


    Java Code:
    public class METHODS_CelsiusTemperatureTable {
    
        /**
         * @param args the command line arguments
         */
        public static void main(String[] args) {
            System.out.println("Fahrenheit\tCelsius");
            System.out.println("--------------------------");
             
            double value = 0;
            
            double fahrenheit = celsius(value);
            
           while (value <= 20){
                System.out.println(value + "\t\t" + fahrenheit);
                value++;
            }
        }
        
        public static double celsius(double temp){
            
            double result = (temp-32) * (5/9); // result is a local variable
            
            return result;
        }
    }

  2. #2
    lenois is offline Member
    Join Date
    Feb 2012
    Posts
    59
    Rep Power
    0

    Default Re: New to forums (and need help)

    Please go through the Forum Rules, particularly the third paragraph.

    Gonna get in before daryll
    harshit shah likes this.

  3. #3
    lenois is offline Member
    Join Date
    Feb 2012
    Posts
    59
    Rep Power
    0

    Default Re: New to forums (and need help)

    check on how many times your while loop reiterates, and on what it is operating. I don't think it is working how you intended.

    Java Code:
    public class METHODS_CelsiusTemperatureTable {
     
        /**
         * @param args the command line arguments
         */
        public static void main(String[] args) {
            //System.out.println("Fahrenheit\tCelsius");
            //System.out.println("--------------------------");
              
            double value = 0;
             
            double fahrenheit = celsius(value);
             
           while (value <= 20){
                System.out.println(value + "\t\t" + fahrenheit);
                value++;
            }
        }
         
        public static double celsius(double temp){
             
            double result = (temp-32) * (5/9); // result is a local variable
            System.out.println(result); 
            return result;
        }
    }
    try this

  4. #4
    lenois is offline Member
    Join Date
    Feb 2012
    Posts
    59
    Rep Power
    0

    Default Re: New to forums (and need help)

    also try passing your expression values manually instead of temp
    Lastly remember how java determines a data type. try just result=(temp-32) and just result=(5/9)
    Last edited by lenois; 02-07-2013 at 06:37 AM.

  5. #5
    smoovejayy is offline Jonathan
    Join Date
    Feb 2013
    Location
    Bronx, NY
    Posts
    10
    Rep Power
    0

    Default Re: New to forums (and need help)

    i don't understand...

    two results?

  6. #6
    lenois is offline Member
    Join Date
    Feb 2012
    Posts
    59
    Rep Power
    0

    Default Re: New to forums (and need help)

    no try them one at a time to diagnose the problem

  7. #7
    lenois is offline Member
    Join Date
    Feb 2012
    Posts
    59
    Rep Power
    0

    Default Re: New to forums (and need help)

    remember that operation on a data type are stored as that data type.

  8. #8
    smoovejayy is offline Jonathan
    Join Date
    Feb 2013
    Location
    Bronx, NY
    Posts
    10
    Rep Power
    0

    Default Re: New to forums (and need help)

    But that's the equation for converting fahrenheit temperatures to celsius.

  9. #9
    lenois is offline Member
    Join Date
    Feb 2012
    Posts
    59
    Rep Power
    0

    Default Re: New to forums (and need help)

    Yes it should be, but it's not. test the two things and check the result.

    try this

    Java Code:
    double test1=(0-32);
    		double test2=(5/9);
    		System.out.println("0-32="+test1+"\n 5/9="+test2);
    Last edited by lenois; 02-07-2013 at 06:57 AM.

  10. #10
    lenois is offline Member
    Join Date
    Feb 2012
    Posts
    59
    Rep Power
    0

    Default Re: New to forums (and need help)

    did you figure it out?

  11. #11
    smoovejayy is offline Jonathan
    Join Date
    Feb 2013
    Location
    Bronx, NY
    Posts
    10
    Rep Power
    0

    Default Re: New to forums (and need help)

    Quote Originally Posted by lenois View Post
    Yes it should be, but it's not. test the two things and check the result.

    try this

    Java Code:
    double test1=(0-32);
    		double test2=(5/9);
    		System.out.println("0-32="+test1+"\n 5/9="+test2);
    Declare those variables in the main method or celsius?

  12. #12
    lenois is offline Member
    Join Date
    Feb 2012
    Posts
    59
    Rep Power
    0

    Default Re: New to forums (and need help)

    doesn't matter as long as you see the results.
    they will work anywhere, since i declared them inline

  13. #13
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,765
    Blog Entries
    7
    Rep Power
    21

    Default Re: New to forums (and need help)

    Try this: System.out.println(5/9) and be surprised ... next, try to explain how you got tha result.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  14. #14
    smoovejayy is offline Jonathan
    Join Date
    Feb 2013
    Location
    Bronx, NY
    Posts
    10
    Rep Power
    0

    Default Re: New to forums (and need help)

    Quote Originally Posted by JosAH View Post
    Try this: System.out.println(5/9) and be surprised ... next, try to explain how you got tha result.

    kind regards,

    Jos
    This is the question: "The formula for converting a temperature from Fahrenheit to Celsius is C=(5/9) * (F-32) where F is the Fahrenheit temperature and C is the Celsius temperature. Write a method named celsius that accepts a Fahrenheit temperature as an argument. The method should return the temperature, converted to Celsius. Demonstrate the method by calling it in a loop that displays a table of the Fahrenheit temperatures 0 through 20 and their Celsius equivalents."

    If the method is supposed to return a value, then a return statement has to be declared, right? Which would imply the method is supposed to return the converted Fahrenheit temperature?

  15. #15
    smoovejayy is offline Jonathan
    Join Date
    Feb 2013
    Location
    Bronx, NY
    Posts
    10
    Rep Power
    0

    Default Re: New to forums (and need help)

    I use NetBeans if that's any help... Does the IDE matter?

  16. #16
    lenois is offline Member
    Join Date
    Feb 2012
    Posts
    59
    Rep Power
    0

    Default Re: New to forums (and need help)

    your program is outputting the correct values for what you gave it, check the math we have told you to check. Your data types are wrong.

  17. #17
    lenois is offline Member
    Join Date
    Feb 2012
    Posts
    59
    Rep Power
    0

    Default Re: New to forums (and need help)

    Quote Originally Posted by smoovejayy View Post
    This is the question: "The formula for converting a temperature from Fahrenheit to Celsius is C=(5/9) * (F-32) where F is the Fahrenheit temperature and C is the Celsius temperature. Write a method named celsius that accepts a Fahrenheit temperature as an argument. The method should return the temperature, converted to Celsius. Demonstrate the method by calling it in a loop that displays a table of the Fahrenheit temperatures 0 through 20 and their Celsius equivalents."

    If the method is supposed to return a value, then a return statement has to be declared, right? Which would imply the method is supposed to return the converted Fahrenheit temperature?
    You are returning the value your program is calculating. Your code is doing everything it is supposed to, it's just not doing what you want it to, and there is a good reason for that. Which we are trying to help you with.

  18. #18
    smoovejayy is offline Jonathan
    Join Date
    Feb 2013
    Location
    Bronx, NY
    Posts
    10
    Rep Power
    0

    Default Re: New to forums (and need help)

    So I've been racking my brain as to why this stupid program won't display the desired results. I've discovered it does display either "return (temp-32)" or "return (5/9)"...

  19. #19
    lenois is offline Member
    Join Date
    Feb 2012
    Posts
    59
    Rep Power
    0

    Default Re: New to forums (and need help)

    That is not what i was getting at. I was trying to skirt around the problem so that you could figure it out for yourself, I am still not giving you the answer but I will isolate your problems.

    1. (5/9) does not equal what you think it does you are trying to make two ints into a floating point number java will just round to keep its data type, which is not what you want.

    double result = (temp-32) * (5/9);
    result does indeed equal 0.0 no matter what temp is

    2. you call your method outside of the loop, meaning that you pass it one value.

    try to fix one, and then you will see what i mean

  20. #20
    smoovejayy is offline Jonathan
    Join Date
    Feb 2013
    Location
    Bronx, NY
    Posts
    10
    Rep Power
    0

    Default Re: New to forums (and need help)

    Sorry for such a late response. School + work keeps me very busy. I will update in a day or two on this program. I want to learn this stuff so badly :D

Similar Threads

  1. forums.sun.com
    By angryboy in forum Entertainment
    Replies: 16
    Last Post: 08-11-2009, 01:15 PM
  2. AI Forums
    By Eranga in forum Suggestions & Feedback
    Replies: 1
    Last Post: 01-31-2009, 06:58 AM
  3. Hello I am new to the forums
    By sari in forum Introductions
    Replies: 0
    Last Post: 01-20-2009, 01:19 AM
  4. New to the Forums
    By uncommon in forum Introductions
    Replies: 2
    Last Post: 12-18-2008, 02:18 PM
  5. Hi, Java Forums
    By SolidCobra in forum Introductions
    Replies: 1
    Last Post: 10-06-2008, 02:37 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •