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

Thread: convert double to int in ArryList

  1. #1
    Member
    Join Date
    Jul 2013
    Posts
    46
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default convert double to int in ArryList

    Hello,

    assume that i have the flowing arrayList:

    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
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: convert double to int in ArryList

    This thread has been cross posted here:

    http://www.java-forums.org/new-java/80627-convert-double-int-arrylist.html

    Although cross posting is allowed, for everyone's benefit, please read:

    Java Programming Forums Cross Posting Rules

    The Problems With Cross Posting

    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    Member
    Join Date
    Jul 2013
    Posts
    46
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: convert double to int in ArryList

    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
            }
        }
        }
    }

  4. #4
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: convert double to int in ArryList

    "how to do it correctly" does not explain very much about the problem you face.
    Is there an error?
    Is the array just left unchanged, where you expected to see a change?

    The line of code (int)arr.get(i); has no side effect, did you expect this to modify a value in the array?

  5. #5
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,545
    My Mood
    Grumpy
    Thanks
    0
    Thanked 167 Times in 158 Posts

    Default Re: convert double to int in ArryList

    Simple answer is DO NOT insert different types into the same List.
    Improving the world one idiot at a time!

  6. #6
    Member
    Join Date
    Jul 2013
    Posts
    46
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: convert double to int in ArryList

    Simple answer is DO NOT insert different types into the same List
    Some times you don't have any another choice, any way the solution to do it

    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));
    }
     
    }

  7. #7
    Member llowe29's Avatar
    Join Date
    Jul 2013
    Posts
    116
    My Mood
    Tired
    Thanks
    9
    Thanked 5 Times in 5 Posts

    Default Re: convert double to int in ArryList

    Please read problems with spoonfeeding.

  8. #8
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: convert double to int in ArryList

    Quote Originally Posted by llowe29 View Post
    Please read problems with spoonfeeding.
    He was posting the solution to his own problem. The user who posted the solution *is* the OP. This is actually pretty okay.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  9. #9
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: convert double to int in ArryList

    Quote Originally Posted by llowe29 View Post
    Please read problems with spoonfeeding.
    You're funny, or a mess. Not sure which.

  10. #10
    Member llowe29's Avatar
    Join Date
    Jul 2013
    Posts
    116
    My Mood
    Tired
    Thanks
    9
    Thanked 5 Times in 5 Posts

    Default Re: convert double to int in ArryList

    I didn't realize it was his own thread sorry lol.

Similar Threads

  1. Need help converting from int to double
    By newmanj8 in forum What's Wrong With My Code?
    Replies: 7
    Last Post: March 26th, 2013, 04:24 PM
  2. How to create an Int or double of what the user types int a JTextField?
    By Speedstack79 in forum Object Oriented Programming
    Replies: 2
    Last Post: January 13th, 2013, 11:00 PM
  3. [SOLVED] double to int problem...
    By proxyspam in forum What's Wrong With My Code?
    Replies: 18
    Last Post: December 1st, 2012, 04:24 PM
  4. How to convert from type double to type int?
    By rph in forum Object Oriented Programming
    Replies: 7
    Last Post: July 25th, 2011, 04:21 AM
  5. int / double validation
    By exnuke1972 in forum What's Wrong With My Code?
    Replies: 4
    Last Post: January 20th, 2011, 05:08 PM