Results 1 to 6 of 6
Thread: Noob Java Math Question...
- 01-30-2013, 11:27 PM #1
Member
- Join Date
- Jan 2013
- Posts
- 7
- Rep Power
- 0
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); } }
Thanks,
TomLast edited by tomtrom77; 01-31-2013 at 03:53 PM. Reason: I just learned about code tags!
- 01-30-2013, 11:30 PM #2
Member
- Join Date
- Jan 2013
- Posts
- 7
- Rep Power
- 0
- 01-31-2013, 02:36 PM #3
Member
- Join Date
- Dec 2012
- Location
- Des Moines, IA
- Posts
- 35
- Rep Power
- 0
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
- 01-31-2013, 03:05 PM #4
Moderator
- Join Date
- Apr 2009
- Posts
- 13,541
- Rep Power
- 26
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 **
- 01-31-2013, 03:28 PM #5
Member
- Join Date
- Jan 2013
- Posts
- 7
- Rep Power
- 0
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
- 01-31-2013, 03:40 PM #6
Member
- Join Date
- Jan 2013
- Posts
- 7
- Rep Power
- 0
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 03:52 PM.
Similar Threads
-
very noob question
By lams_007 in forum New To JavaReplies: 4Last Post: 01-30-2013, 10:24 PM -
noob question!
By swp in forum New To JavaReplies: 5Last Post: 09-29-2011, 12:14 AM -
noob question. plz help tho!
By swp in forum New To JavaReplies: 2Last Post: 09-28-2011, 07:16 AM -
Probably Really Noob Question
By bpx95 in forum New To JavaReplies: 2Last Post: 05-16-2011, 02:44 AM -
Help im a noob.. a super noob on java..
By critdevil in forum New To JavaReplies: 12Last Post: 03-07-2009, 04:17 AM
Bookmarks