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

Thread: Adding doubles and ints.

  1. #1
    Junior Member
    Join Date
    Aug 2011
    Posts
    25
    My Mood
    Bored
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Adding doubles and ints.

    First of all, I hate the fact Java can't just NOT separate the two. It's a bit annoying to keep up with. But anyway:

    I have a stack of strings. Some are "4.4" and some are "1".

    When they are "popped" they are then converted to a double or an integer depending on certain circumstances that are probably obvious here.

    The stack looks like this:

    4.4 1 +

    They are to be popped and added, but I have a problem right there.
    I have no idea how to add 4.4 and 1 and get an exact answer that isn't rounded down or anything like that. So what do I do?


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

    Default Re: Adding doubles and ints.

    What have you tried?
    System.out.println(4.4 + 1);
    What does that output?

    Why are you bothering with ints? Why not just convert everything to double?
    Improving the world one idiot at a time!

  3. #3
    Junior Member
    Join Date
    Aug 2011
    Posts
    25
    My Mood
    Bored
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Adding doubles and ints.

    I actually didn't know you could do that with the System.out.println command. Sorry, I'm still a bit new at this, I've only known Java for like a month.

    Edit
    Actually, that wouldn't work if 4.4+1 is part of a bigger expression.
    You're right about the doubles though.
    Last edited by SkyAphid; September 7th, 2011 at 10:02 PM.

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

    Default Re: Adding doubles and ints.

    It has nothing to do with the println method and everything to do with math.
    int one = 1;
    double fourPointFour = 4.4;
    double result = fourPointFour + one;
    Works just fine too.
    Improving the world one idiot at a time!

  5. #5
    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: Adding doubles and ints.

    Quote Originally Posted by SkyAphid View Post
    First of all, I hate the fact Java can't just NOT separate the two. It's a bit annoying to keep up with.
    The two serve different purposes, and people who really care about memory don't want to use something they don't need. "NOT separating the two" would be a horrible idea.
    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!

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Adding doubles and ints.

    It's a bit annoying to keep up with
    You will find that a lot in programming.There are Lots of important details to keep up with.

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

    Default Re: Adding doubles and ints.

    Quote Originally Posted by SkyAphid View Post
    Actually, that wouldn't work if 4.4+1 is part of a bigger expression.
    What the heck are you talking about? 4.4 + 1 will produce 5.4 every single time regardless of where you place it in your code. Unless you have 4.4 + 1 * 3.2 in your expression. That of course would produce a different result due to operator precedence but the result will still be a double and you can still add, multiply, subtract and divide ints and doubles.
    Improving the world one idiot at a time!

Similar Threads

  1. Addition of doubles (newbie question)
    By archyb in forum Java Theory & Questions
    Replies: 2
    Last Post: April 21st, 2011, 09:52 AM
  2. Externally sorting ints as fast as possible
    By Djikstrangle in forum Algorithms & Recursion
    Replies: 4
    Last Post: September 21st, 2010, 03:00 PM
  3. adding multiple ints into a string
    By straw in forum Java Theory & Questions
    Replies: 1
    Last Post: March 18th, 2010, 06:02 PM
  4. New person Just trying to read a file of ints
    By dubois.ford in forum File I/O & Other I/O Streams
    Replies: 1
    Last Post: March 7th, 2010, 11:47 PM
  5. Reading doubles from file
    By fawx in forum File I/O & Other I/O Streams
    Replies: 1
    Last Post: October 24th, 2009, 11:57 PM