Results 1 to 6 of 6
  1. #1
    tomtrom77 is offline Member
    Join Date
    Jan 2013
    Posts
    7
    Rep Power
    0

    Default Noob Java Math Question...

    I am working through the Java course over at wibit.net (GREAT programming tutorials!) Given that their forums seem to be locked for the moment, I have a question about one of their String examples here:

    Java Code:
    import java.util.Random;
    
    class StringExample3
    {
    	public static void main(String[] args)
    	{
    		Random rand = new Random();
    		String outputStr = "";
    		Integer n1 = new Integer(rand.nextInt());
    		Integer n2 = new Integer(rand.nextInt());
    		Integer n3 = new Integer(n1.intValue() + n2.intValue());
    		outputStr = n1.toString() + " + " + n2.toString() + " = " + n3.toString();
    		System.out.println(outputStr);
    	}
    }
    So when I run the program, most of the time n1 + n2 really does = n3. However, about one in 10 times, it does not. Basically, the output will sometimes look like this: 1603065481 + 1105661559 = -1586240256. This is clearly not right. Like I said, it's a one in 10 occurrence, but this has me wondering. Can someone explain what is going on here? I doubt that there is a math issue within the language itself, or is there? What am I missing?

    Thanks,

    Tom
    Last edited by tomtrom77; 01-31-2013 at 02:53 PM. Reason: I just learned about code tags!

  2. #2
    tomtrom77 is offline Member
    Join Date
    Jan 2013
    Posts
    7
    Rep Power
    0

    Default Re: Noob Java Math Question...

    Noob Java Math Question...-java-question.jpg

  3. #3
    foulkelore is offline Member
    Join Date
    Dec 2012
    Location
    Des Moines, IA
    Posts
    35
    Rep Power
    0

    Default Re: Noob Java Math Question...

    You might try using long or double instead in int. I got the following info from the JAVA Tutorials

    minimum value of -2,147,483,648 and a maximum value of 2,147,483,647

  4. #4
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,931
    Rep Power
    19

    Default Re: Noob Java Math Question...

    Yep.
    Your ints are wrapping, so use longs or change to using BigIntegers if you can't guarantee a long will be big enough.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  5. #5
    tomtrom77 is offline Member
    Join Date
    Jan 2013
    Posts
    7
    Rep Power
    0

    Default Re: Noob Java Math Question...

    Ah! That make sense!

    This is one of those things that I have learned but hadn't ever seen in practice, so I didn't recognize it when it popped up!

    I appreciate your help!

    -Tom

  6. #6
    tomtrom77 is offline Member
    Join Date
    Jan 2013
    Posts
    7
    Rep Power
    0

    Default Re: Noob Java Math Question...

    Here is the revised and now-properly-functioning code:

    Java Code:
    import java.util.Random;
    
    class StringExample3
    {
    	public static void main(String[] args)
    	{
    		Random rand = new Random();
    		String outputStr = "";
    		Integer n1 = new Integer(rand.nextInt());
    		Integer n2 = new Integer(rand.nextInt());
    		Long n3 = new Long((long)n1.intValue() + (long)n2.intValue());
    		outputStr = n1.toString() + " + " + n2.toString() + " = " + n3.toString();
    		System.out.println(outputStr);
    	}
    }
    Last edited by tomtrom77; 01-31-2013 at 02:52 PM.

Similar Threads

  1. very noob question
    By lams_007 in forum New To Java
    Replies: 4
    Last Post: 01-30-2013, 09:24 PM
  2. noob question!
    By swp in forum New To Java
    Replies: 5
    Last Post: 09-28-2011, 11:14 PM
  3. noob question. plz help tho!
    By swp in forum New To Java
    Replies: 2
    Last Post: 09-28-2011, 06:16 AM
  4. Probably Really Noob Question
    By bpx95 in forum New To Java
    Replies: 2
    Last Post: 05-16-2011, 01:44 AM
  5. Help im a noob.. a super noob on java..
    By critdevil in forum New To Java
    Replies: 12
    Last Post: 03-07-2009, 03:17 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
  •