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: Help! Not getting correct output in fahrenheit to centigrade conversion!

  1. #1
    Member
    Join Date
    Jan 2014
    Location
    New Jersey
    Posts
    48
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Help! Not getting correct output in fahrenheit to centigrade conversion!

    Hello:
    I am writing a small program to convert temperature from Fahrenheit to Centigrade. I cannot figure out why I keep getting 0.00 for the Centigrade temperature output. Also, I cannot figure out how to get the F degree symbol before or after the temp. I know in JOptionPane the char = 176 is the code for it but im not sure how to code it into the program. If someone could show me how and also give me some insight why my program keeps saying 0.00 for Centigrade temp on the output that would be great!

    import javax.swing.JOptionPane;
    import java.text.DecimalFormat;
     
     
    public class tempConversion
    {
    	public static void main(String[] args)
    	{
    		String newDecimal;
    		String newDecimal1;
    		String fTemp;
    		double cTemp;
    		double centiGrade;
    		double fahrenHeit;
    		double finalC;
     
    		fTemp =
    			JOptionPane.showInputDialog("Please enter a temperature in F: ");
    			fahrenHeit = Double.parseDouble(fTemp);
     
    		cTemp = (5 / 9) * (fahrenHeit - 32);
     
    		DecimalFormat formatter = new DecimalFormat("#0.00");
     
    		newDecimal = (formatter.format(fahrenHeit));
    		newDecimal1 = (formatter.format(cTemp));
     
     
    		JOptionPane.showMessageDialog(null, "Current temp: " + newDecimal
    					     			 + " F\nCurrent temp: " + newDecimal1 + " C");
    	}
    }


    --- Update ---

    Okay I figured out the Degree symbol!(String DEGREE = "\u00b0") phew... now only if i can get the correct output!


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    22,321
    Thanks
    56
    Thanked 2,374 Times in 2,345 Posts

    Default Re: Help! Not getting correct output in fahrenheit to centigrade conversion!

    The code uses integer math: 5/9 = 0
    Change one of those to double: 5.0/9 to get a non-zero result
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Member
    Join Date
    Jan 2014
    Location
    New Jersey
    Posts
    48
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Help! Not getting correct output in fahrenheit to centigrade conversion!

    BOOM! thank you!

Similar Threads

  1. Cannot get the correct output
    By akif13 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: December 6th, 2013, 04:15 AM
  2. For loop not giving the correct output
    By Kattracks32 in forum Loops & Control Statements
    Replies: 1
    Last Post: February 28th, 2013, 04:41 AM
  3. Not getting the correct output
    By Ashish S in forum What's Wrong With My Code?
    Replies: 1
    Last Post: December 26th, 2012, 03:56 AM
  4. Not getting the correct output
    By KNAYERS in forum What's Wrong With My Code?
    Replies: 3
    Last Post: November 23rd, 2012, 01:59 PM
  5. [SOLVED] Trouble with fahrenheit to celsius conversion.
    By HaHom Tsa Pfnuma in forum What's Wrong With My Code?
    Replies: 3
    Last Post: July 27th, 2012, 10:50 AM