Results 1 to 5 of 5
 10062013, 01:18 AM #1Member
 Join Date
 Oct 2013
 Posts
 11
 Rep Power
 0
Math.abs possibly returning a negative number
Hi,
the following code gives me an error saying that "n must be positive" but the abs function should only return positive numbers so why do I get this error?
Java Code:y1 = 42  Math.abs(6*(i7)); y2 = 21  Math.abs(3*(i7)); int range = Math.abs(y1  y2); dirt = land.nextInt(range) + y2;
 10062013, 01:56 AM #2Senior Member
 Join Date
 Jan 2013
 Location
 Northern Virginia, United States
 Posts
 6,226
 Rep Power
 14
Re: Math.abs possibly returning a negative number
Not enough information provided. You need to provide an Short, Self Contained, Correct Example and the error message. Otherwise, we are just guessing.
Regards,
JimThe Java^{TM} Tutorials  SSCCE  Java Naming Conventions
Poor planning on your part does not constitute an emergency on my part
 10062013, 02:15 AM #3Member
 Join Date
 Oct 2013
 Posts
 11
 Rep Power
 0
Re: Math.abs possibly returning a negative number
Hi,
The code I posted is the only code that pertains to the problem. It is part of a larger method that is used in the main method. here is the error it gives me:
Exception in thread "main" java.lang.IllegalArgumentException: n must be positive
at java.util.Random.nextInt(Random.java:250)
at mapGenerator.island(mapGenerator.java:39)
at mapGenerator.main(mapGenerator.java:15)
I know what the error is saying (the random function can not take a negative value as an argument). the problem I have is that according to my code the argument should never be negative. so why does it tell me that it is getting a negative argument? when I assign the variable range as the value of Math.abs(6) it runs just fine. but when it is Math.abs(y1  y2) it gives me the error.
 10062013, 02:26 AM #4Senior Member
 Join Date
 Jan 2013
 Location
 Northern Virginia, United States
 Posts
 6,226
 Rep Power
 14
Re: Math.abs possibly returning a negative number
No you still need to provide more code. What is the value of i? We certainly don't know. Perhaps range is zero. Did you print it out?
Regards,
JimThe Java^{TM} Tutorials  SSCCE  Java Naming Conventions
Poor planning on your part does not constitute an emergency on my part
 10062013, 02:31 AM #5Member
 Join Date
 Oct 2013
 Posts
 11
 Rep Power
 0
Similar Threads

Math.sqRt and negative numbers
By SkoodlyWah in forum New To JavaReplies: 4Last Post: 07192013, 10:07 AM 
Counting positive number inputs and negative number inputs of user. HELP!
By cs3 in forum New To JavaReplies: 1Last Post: 10242012, 12:21 PM 
reversing number cant run negative number
By irnie1994 in forum JCreatorReplies: 2Last Post: 10302011, 05:49 PM 
Find the First Negative Number of an Array
By Cod in forum New To JavaReplies: 12Last Post: 02202011, 05:59 PM 
[SOLVED] Array of first negative number
By random0munky in forum New To JavaReplies: 9Last Post: 12082008, 11:17 PM
Bookmarks