Results 1 to 12 of 12

Thread: Strange Error

  1. #1
    Zelaine is offline Senior Member
    Join Date
    Aug 2013
    Location
    Sweden
    Posts
    152
    Rep Power
    1

    Default Strange Error

    Hello!

    My program is supposed to convert degrees Fahrenheit to degrees Celsius, but somehow it always outputs 0, and I don't know why. Maybe you do?

    Java Code:
    import javax.swing.*;
    
    public class attakomma1{
    	public static void main(String[] args){
    		String s = JOptionPane.showInputDialog("Enter a temperature in degrees Fahrenheit");
    		double temperatur = Double.parseDouble(s);
    		temperatur = (temperatur-32)*(5/9);
    		JOptionPane.showMessageDialog(null, "Here is the temperature in degrees Celsius: " + temperatur);
    	}
    }

  2. #2
    Kuttappu's Avatar
    Kuttappu is offline Member
    Join Date
    Aug 2011
    Location
    Kerala, India
    Posts
    40
    Rep Power
    0

    Default Re: Strange Error

    problem is in 5/9
    it will give answer as 0, since it is integer division..
    you can solve it by replacing that statement by

    temperatur = (temperatur-32)*((double)5/(double)9);

  3. #3
    Zelaine is offline Senior Member
    Join Date
    Aug 2013
    Location
    Sweden
    Posts
    152
    Rep Power
    1

    Default Re: Strange Error

    Oh, thanks :) Isn't just writing (double)5/9 sufficient?

  4. #4
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,501
    Rep Power
    5

    Default Re: Strange Error

    Why not just write 5/9.0

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  5. #5
    quad64bit's Avatar
    quad64bit is offline Moderator
    Join Date
    Jul 2009
    Location
    VA
    Posts
    1,323
    Rep Power
    7

    Default Re: Strange Error

    Isn't just writing (double)5/9 sufficient?
    No. What you get there is 5.0/9 which is a double divided by an integer which becomes integer division.

    5/9.0
    that Jim suggested is integer divided by float, which will result in a float point division. When in doubt, cast everything to the type you expect to work with - but understanding when and how integer division works in java is helpful.

  6. #6
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,501
    Rep Power
    5

    Default Re: Strange Error

    Uh, actually (double)5/9 or 5.0/9 also works.

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  7. #7
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,779
    Rep Power
    7

    Default Re: Strange Error

    5.0 / 9

    The 9 will be promoted to a double before the division is performed. Thus giving a double result. Java will never lose precision unless you tell it via a cast or some other truncating method.

  8. #8
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    3,841
    Rep Power
    5

    Default Re: Strange Error

    Quote Originally Posted by Kuttappu View Post
    It wont work. Answer for it will be 0.0
    Did you try it, or are you just assuming? Because I tried it, and it works just fine. You are assuming here that the cast will be for the result of the equation, but it is in fact casting the 5 into a double 5.0.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  9. #9
    Kuttappu's Avatar
    Kuttappu is offline Member
    Join Date
    Aug 2011
    Location
    Kerala, India
    Posts
    40
    Rep Power
    0

    Default Re: Strange Error

    Quote Originally Posted by quad64bit View Post
    No. What you get there is 5.0/9 which is a double divided by an integer which becomes integer division.


    that Jim suggested is integer divided by float, which will result in a float point division. When in doubt, cast everything to the type you expect to work with - but understanding when and how integer division works in java is helpful.

    both will work actually. you just need one of the operands to be in double, doesnt matter if it is 5.0/9 or 5/9.0

  10. #10
    Kuttappu's Avatar
    Kuttappu is offline Member
    Join Date
    Aug 2011
    Location
    Kerala, India
    Posts
    40
    Rep Power
    0

    Default Re: Strange Error

    Quote Originally Posted by gimbal2 View Post
    Did you try it, or are you just assuming? Because I tried it, and it works just fine. You are assuming here that the cast will be for the result of the equation, but it is in fact casting the 5 into a double 5.0.
    yes. u are right, it works.. I tried like (double)(5/9).. My bad..

  11. #11
    quad64bit's Avatar
    quad64bit is offline Moderator
    Join Date
    Jul 2009
    Location
    VA
    Posts
    1,323
    Rep Power
    7

    Default Re: Strange Error

    Oh whoops - good catch. Guess I should try things before talking about them aye? Good life lesson in that :D

  12. #12
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    3,841
    Rep Power
    5

    Default Re: Strange Error

    Quote Originally Posted by quad64bit View Post
    Oh whoops - good catch. Guess I should try things before talking about them aye? Good life lesson in that :D
    Don't beat yourself up. Being a 64 bit quadcore processor, you have a specialized embedded chip with matching instruction set to handle that stuff for you.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

Similar Threads

  1. Strange Error
    By willemien in forum AWT / Swing
    Replies: 24
    Last Post: 04-19-2013, 07:26 PM
  2. strange error in eclipse
    By stevenpalomino in forum New To Java
    Replies: 29
    Last Post: 06-25-2011, 01:46 PM
  3. Strange Error
    By AJArmstron@aol.com in forum New To Java
    Replies: 1
    Last Post: 04-18-2010, 09:31 PM
  4. Strange, 'Cannot find symbol' error
    By mhouldridge in forum Advanced Java
    Replies: 6
    Last Post: 09-06-2009, 04:54 PM
  5. strange Error message
    By little_polarbear in forum New To Java
    Replies: 4
    Last Post: 08-25-2008, 11:45 PM

Posting Permissions

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