Results 1 to 3 of 3
Like Tree1Likes
  • 1 Post By jim829

Thread: Ints and Doubles?

  1. #1
    Galway is offline Member
    Join Date
    Sep 2016
    Posts
    15
    Rep Power
    0

    Default Ints and Doubles?

    public class changetype{
    public static void main(String[]args){
    int x=10;
    x=0.5*x;
    System.out.print(x);
    }
    }

    This is a sample class(I think its a class?) in java as I wanted to try and manipulate integers like in the line I have highlighted.
    I have tried a to get this to work but can't. Read some were to try
    x=intValue(0.5*x);
    Yet this also failed and said " error: incompatible types: possible lossy conversion from double to int"

    Any tips on how to get this to work would be appreciated just started Java in University 2 weeks ago.

  2. #2
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    14

    Default Re: Ints and Doubles?

    You need to cast the result to an int. (int)(0.5*x)

    Regards,
    Jim
    Galway likes this.
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  3. #3
    Galway is offline Member
    Join Date
    Sep 2016
    Posts
    15
    Rep Power
    0

    Default Re: Ints and Doubles?

    Just wanted to say thank you, it worked. I had tried int(0.5*x) trying to cast previously but did not know about the extra brackets.

Similar Threads

  1. Add doubles in an array
    By maxiffer in forum New To Java
    Replies: 27
    Last Post: 01-20-2014, 02:39 PM
  2. Identifying Ints and Non Ints in a string
    By Mnelson in forum New To Java
    Replies: 6
    Last Post: 07-07-2012, 12:08 AM
  3. Arithmetic using doubles
    By Black_Eye in forum New To Java
    Replies: 4
    Last Post: 10-19-2010, 08:35 AM
  4. Cant use doubles
    By Freakzoyd in forum New To Java
    Replies: 27
    Last Post: 07-24-2010, 04:48 AM
  5. GUI's and inputting doubles or ints
    By lopder1 in forum New To Java
    Replies: 19
    Last Post: 11-05-2009, 08:50 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
  •