Results 1 to 16 of 16
Like Tree2Likes
  • 2 Post By Tolls

Thread: convert double to int in ArryList

  1. #1
    vector_ever is offline Senior Member
    Join Date
    Jul 2013
    Posts
    187
    Rep Power
    2

    Default convert double to int in ArryList

    Hello,

    assume that i have the flowing arrayList:

    Java Code:
    ArrayList arr = new arrayList();
    arr.add("hi");
    arr.add(2.5);
    arr.add("x");
    arr.add(2.9);
    arr.add(1.0);
    now i want to convert every double value in arr to integer, how to do that (just the double and ignore another types)

  2. #2
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    4,017
    Rep Power
    10

    Default Re: convert double to int in ArryList

    What have you tried?
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    4,017
    Rep Power
    10

    Default Re: convert double to int in ArryList

    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  4. #4
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,183
    Rep Power
    20

    Default Re: convert double to int in ArryList

    Well, one way would be to not make the mistake of populating your list with seemingly random datatypes.
    DarrylBurke and gimbal2 like this.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  5. #5
    vector_ever is offline Senior Member
    Join Date
    Jul 2013
    Posts
    187
    Rep Power
    2

    Default Re: convert double to int in ArryList

    Java Code:
    	public static void main(String[] args) {
    		ArrayList arr = new ArrayList();
    		arr.add("hi");
    		arr.add(2.5);
    		arr.add("x");
    		arr.add(2.9);
    		arr.add(1.0);
    
    		for(int i = 0; i < arr.size(); i++){
    		if (arr.get(i) instanceof Double ){
    			(int)arr.get(i); //false, but how to do it correctly
    		}
    	}
    	}
    }

  6. #6
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    4,277
    Rep Power
    6

    Default Re: convert double to int in ArryList

    Have you considered checking out the API documentation of the Double class? Perhaps there is a method in there that does what you want.

    Google: java 7 Double
    Result: Double (Java Platform SE 7 )
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  7. #7
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,730
    Blog Entries
    7
    Rep Power
    21

    Default Re: convert double to int in ArryList

    Why did you put those Doubles in that (raw) List in the first place? Couldn't you have checked and casted them before you put them in that List?

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  8. #8
    vector_ever is offline Senior Member
    Join Date
    Jul 2013
    Posts
    187
    Rep Power
    2

    Default Re: convert double to int in ArryList

    Google: java 7 Double
    Result: Double (Java Platform SE 7 )
    Thank Erik, i read it but not new in it, still have no soultion

    Why did you put those Doubles in that (raw) List in the first place? Couldn't you have checked and casted them before you put them in that List?
    hi jos, i just looking a solution to this case, jut when they are already in the arraylist
    Last edited by vector_ever; 08-05-2013 at 05:06 PM.

  9. #9
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,183
    Rep Power
    20

    Default Re: convert double to int in ArryList

    But that's your problem.
    Not how to identify them and convert them, but to not have them there in the first place.
    You shouldn't be in this position.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  10. #10
    vector_ever is offline Senior Member
    Join Date
    Jul 2013
    Posts
    187
    Rep Power
    2

    Default Re: convert double to int in ArryList

    your solution is proper just if i have another choice, i have library an it read some parameter from the internal network and store them in ArrayList, and i have no access to these Parameters before add them to arraylist

  11. #11
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    4,017
    Rep Power
    10

    Default Re: convert double to int in ArryList

    Again, the API is your best friend: ArrayList (Java Platform SE 7 )

    You could also build a new ArrayList, but as others have said, this whole thing has a pretty bad code smell.
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  12. #12
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    4,277
    Rep Power
    6

    Default Re: convert double to int in ArryList

    Quote Originally Posted by vector_ever View Post
    your solution is proper just if i have another choice, i have library an it read some parameter from the internal network and store them in ArrayList, and i have no access to these Parameters before add them to arraylist
    Now hopefully you know why hiding information only makes it more difficult for people to help you. If you would have told from the very beginning where that ArrayList is coming from, then I'm guessing that Tolls and JosAH wouldn't have wasted their time to help you correct your requirements in stead of the code. As you present it now, we could have only assumed that you are creating this ArrayList yourself.

    Back to the matter at hand. Your current problem state:

    - you have an Object (arr.get(i) returns an Object); you need a Double. You must know about type casting since you have that (int) in your code
    - apparently when you have your Double, you actually want to convert it to an int, or else I would understand what that (int) typecast is for

    The first you should be able to solve yourself, the second really can be solved by taking another look at the Double API. There is a method in there that can help you. I'm not going to spell it out, it is high time that you learn how to find (trivial) information for yourself.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  13. #13
    vector_ever is offline Senior Member
    Join Date
    Jul 2013
    Posts
    187
    Rep Power
    2

    Default Re: convert double to int in ArryList

    I don't know why you think that i hide the information or try to waste every body's time !!

    Any way to solve my self as you mean i talk second and third look at Double API, i don't find anything i can use it in my question unless if you mean .intValue(), because i test it with my code and nothing changed
    Java Code:
            for(int i = 0; i < arr.size(); i++){
            if (arr.get(i) instanceof Double ){
                ((Double)arr.get(i)).intValue(); 
            }
    The double don't get changed to int.

  14. #14
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    4,277
    Rep Power
    6

    Default Re: convert double to int in ArryList

    Quote Originally Posted by vector_ever View Post
    I don't know why you think that i hide the information or try to waste every body's time !!
    Try to understand what I'm saying in stead of just assuming the worst.

    i can use it in my question unless if you mean .intValue()
    bingo.


    The double don't get changed to int.
    Well then you did it wrong. Take a little longer to figure it out. Start by cleaning up the code so it is more readable.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  15. #15
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    4,017
    Rep Power
    10

    Default Re: convert double to int in ArryList

    I provided the link to the API for ArrayList. That contains methods you need to use.

    Casting a value does not automatically update any references to that value. For example, if I have something like:

    Java Code:
    double d = 1.3;
    (int)1.3;
    System.out.println(d);
    It's not going to magically make d an int, or even put 1.0 in d. You have to update any references to the value you want to change, including those inside the ArrayList. Hence the link to the API for useful functions.
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  16. #16
    vector_ever is offline Senior Member
    Join Date
    Jul 2013
    Posts
    187
    Rep Power
    2

    Default Re: convert double to int in ArryList

    Java Code:
    	public static void main(String[] args) {
            ArrayList arr = new ArrayList();
            arr.add("hi");
            arr.add(2.5);
            arr.add("x");
            arr.add(2.9);
            arr.add(1.0);
     
            for(int i = 0; i < arr.size(); i++){
            if (arr.get(i) instanceof Double ){
            	 int n = ((Double) arr.get(i)).intValue(); 
            	 arr.set(i, n);
            }
            System.out.println(arr.get(i));
        }
    
        }
    Thanks

Similar Threads

  1. Convert double to Date
    By javaWannabe in forum New To Java
    Replies: 3
    Last Post: 03-29-2012, 11:51 PM
  2. Convert from string to double
    By Lord ice in forum New To Java
    Replies: 4
    Last Post: 12-12-2010, 06:27 PM
  3. How to convert a double into a int?
    By tyang in forum New To Java
    Replies: 4
    Last Post: 02-10-2010, 11:02 AM
  4. convert String to Double
    By azurovyhrosik in forum CLDC and MIDP
    Replies: 5
    Last Post: 10-22-2008, 03:46 AM
  5. convert string to a double?
    By javaMike in forum Advanced Java
    Replies: 2
    Last Post: 11-27-2007, 04:10 AM

Posting Permissions

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