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

Thread: Is it the Math or is it my Methods?

  1. #1
    Member
    Join Date
    Feb 2013
    Posts
    38
    Thanks
    4
    Thanked 1 Time in 1 Post

    Default Is it the Math or is it my Methods?

    Hi all. Ron here. It's been a few weeks. I hope everyone is well.

    What I have here is part of a program I'm working on. What I'm doing at this point is converting a Fahrenheit temperature to Celsius. Simple enough, but every time I run this thing, it keeps returning -0.0, no matter what. I can't figure out if it's the way I've keyed the formula or if I've done something wrong with the methods. Anyone care to chime in?

    import javax.swing.JOptionPane;
     
    public class Celsius
    {
    	public static void main(String[] args)
    	{	
    		String input;
    		double fahrenTemp;
     
    		fahrenTemp = getFahren();
    		convertTemp(fahrenTemp);
    		System.exit(0);
     
    	}
     
    	public static double getFahren()
    	{
    		String input = JOptionPane.showInputDialog("Please enter a Fahrenheit temperature from 0 to 10 "
    												+ "and I will convert it to Celsius for you.");
    		double fahrenTemp = Double.parseDouble(input);
    		return fahrenTemp;
    	}
    	public static void convertTemp(double fahrenTemp)
    	{
    		double toCelsius = (5 / 9) * (fahrenTemp - 32);
    		JOptionPane.showMessageDialog(null, "That would be " + toCelsius + " degrees Celsius.");
     
    	}
     
    }


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Is it the Math or is it my Methods?

    The code is using int arithmetic: 5/10 = 0
    vs 5.0/10 = 0.5

    Use some double values in divisions
    If you don't understand my answer, don't ignore it, ask a question.

  3. The Following User Says Thank You to Norm For This Useful Post:

    Praetorian (April 11th, 2013)

  4. #3
    Member
    Join Date
    Feb 2013
    Posts
    38
    Thanks
    4
    Thanked 1 Time in 1 Post

    Default Re: Is it the Math or is it my Methods?

    Norm! That's it!! You rock! Thank you SO much.

Similar Threads

  1. Why and where abstract methods & classes and static methods are used?
    By ajaysharma in forum Object Oriented Programming
    Replies: 7
    Last Post: July 14th, 2012, 01:16 AM
  2. Math.E - what exactly is it?
    By RedCloudTsunami in forum Java Theory & Questions
    Replies: 2
    Last Post: July 10th, 2012, 12:24 AM
  3. [SOLVED] Help with Math.tan and Math.atan
    By Dr.Code in forum Algorithms & Recursion
    Replies: 6
    Last Post: July 2nd, 2012, 05:54 AM
  4. Confusion with Math.toDegrees() and Math.toRadians(). Please help.
    By marksquall in forum Java Theory & Questions
    Replies: 3
    Last Post: June 23rd, 2011, 01:28 AM
  5. Math Problem
    By kidsforsale in forum What's Wrong With My Code?
    Replies: 11
    Last Post: September 14th, 2010, 04:09 PM