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

Thread: How to convert from type double to type int?

  1. #1
    Junior Member
    Join Date
    Jul 2011
    Posts
    9
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default How to convert from type double to type int?

    I need to convert the result of a calculation from type double to type int, what's the best way of going about this?
    I need to do this because I need the results of the calculation to be the length of a rectangle that will be drawn.
    Many thanks


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,375
    Thanks
    49
    Thanked 2,208 Times in 2,181 Posts

    Default Re: How to convert from type double to type int?

    You can cast it: int i = (int)1.3;

  3. #3
    Junior Member
    Join Date
    Jul 2011
    Posts
    9
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: How to convert from type double to type int?

    Ok I think I got that, but BlueJ is saying that I'm having an error still.

    import java.awt.*;
    import javax.swing.*;
     
    public class goldenrect extends JApplet
    {
    double length;
    public void init()
    {
    String shortSide;
    double shortnumb;
     
    shortSide = 
    JOptionPane.showInputDialog ("Enter the length of the shorter side in pixels");
     
    shortnumb = Integer.parseInt( shortSide );
     
    length = shortnumb * 1.6180339887;
     
    int shortnumb2 = (int) shortnumb;
     
    int length2 = (int) length;
    }
    public void paint ( Graphics g )
    {
    g.drawRect( 15, 10, length2, shortnumb2 );
    g.drawString( "The length of the longer side is " + length, 25, 25);
    }
    }

    It says that it cannot find the symbol- variable length2
    Any ideas? Any help is greatly appreciated

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,375
    Thanks
    49
    Thanked 2,208 Times in 2,181 Posts

    Default Re: How to convert from type double to type int?

    What line is the error on?

    Is length2 a local variable in a method?
    or is it a class variable know to all methods?

    Local variables are only known in the method (or enclosing {}s) they are defined in.

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

    rph (July 21st, 2011)

  6. #5
    mmm.. coffee JavaPF's Avatar
    Join Date
    May 2008
    Location
    United Kingdom
    Posts
    3,336
    My Mood
    Mellow
    Thanks
    258
    Thanked 287 Times in 225 Posts
    Blog Entries
    4

    Default Re: How to convert from type double to type int?

    Please use [highlight=Java] code [/highlight] tags when posting your code.
    Forum Tip: Add to peoples reputation by clicking the button on their useful posts.

    Looking for a Java job? Visit - Java Programming Careers

  7. #6
    Junior Member
    Join Date
    Jul 2011
    Posts
    9
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: How to convert from type double to type int?

    Yeah that's what the problem was, I hadn't declared length2 as a class variable, thanks
    However now I have the problem that the conversion from double to int makes the int value 0, any ideas why this might be?


    import java.awt.*;
    import javax.swing.*;

    public class goldenrect extends JApplet
    {
    double length, shortnumb;
    int length2, shortnumb2;
    String shortSide;
    public void init()
    {
    String shortSide;
    double shortnumb;

    shortSide =
    JOptionPane.showInputDialog ("Enter the length of the shorter side in pixels");

    shortnumb = Double.parseDouble( shortSide );

    length = shortnumb * 1.6180339887;

    int shortnumb2 = (int) shortnumb;

    int length2 = (int) length;

    }
    public void paint ( Graphics g )
    {

    g.drawRect( 15, 10, length2, shortnumb2 );
    g.drawString( "The length of the longer side is " + length, 25, 25);
    }
    }

  8. #7
    Junior Member
    Join Date
    Jul 2011
    Posts
    9
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: How to convert from type double to type int?

    Never mind I've figured it out myself, I just had to move

    int shortnumb2 = (int) shortnumb;

    int length2 = (int) length;

    into the public void paint class.
    Thanks for all your help

  9. #8
    Junior Member madelarbo's Avatar
    Join Date
    Jul 2011
    Location
    Muntinlupa City
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: How to convert from type double to type int?

    you can parse.. or change data type into INT.. or cast

Similar Threads

  1. how to read an integer of DOUBLE datatype with type casting
    By amr in forum What's Wrong With My Code?
    Replies: 3
    Last Post: December 14th, 2010, 02:03 PM
  2. Typecasting of double variable to integer
    By JavaPF in forum Java Programming Tutorials
    Replies: 2
    Last Post: December 5th, 2010, 02:41 AM
  3. Typecasting of double variable to integer
    By JavaPF in forum Java Code Snippets and Tutorials
    Replies: 2
    Last Post: December 5th, 2010, 02:41 AM
  4. Not sure what type of variable to use.
    By mjpam in forum Java Theory & Questions
    Replies: 13
    Last Post: July 13th, 2010, 08:58 PM
  5. cannot be resolved to a type
    By Teraphim in forum What's Wrong With My Code?
    Replies: 1
    Last Post: February 16th, 2010, 09:42 AM

Tags for this Thread