participate


New To Java - Java problems....again (Creating random array + its values)
<<   Back to Forum  |   Give us Feedback
5 Duke Stars rewarded for this thread
This topic has 5 replies on 1 page.
A_A_M_I_R
Posts:52
Registered: 2/21/09
Java problems....again (Creating random array + its values)   
Nov 7, 2009 3:39 PM

 
Sorry to bother again, but I have more problems =|

I have a main class where a method is called:
public static void main(String[] args)
{
int[] array1 = ArrayUtils.randomArray(10, 6) ;
}


And then there is the method being called:

Generates a random array
@param n the size of the array
@param range the random numbers go from 0 to range - 1
^ Instructions given by my teacher, below is what i tried
*/ 
public static int[] randomArray(int n, int range) {
Random arraygen = new Random(); // I guess I'd have to create a random variable...
 
int[] array2 = new int[n]; // So this statement 'should' create a random array everytime its called?? (with value 'n' taken when its called in the tester class)
int ranger = arraygen.nextInt(range-1);
 
for (int i=0;i<=array2.length;i++)  // going thru each value of the array, length was 'n'
{
  array2[i] = arraygen.nextInt(ranger); //Create random value for each array item???
//  System.out.println("Random array value " + i + " is " + array1[i]);  //Random values DO get printed if i uncomment this, but then program crashes afterwards
}


Even if I uncomment the 'system.out.println' line near the end, I do get all the random values but at the end it says:
java.lang.ArrayIndexOutOfBoundsException: 10
at ArrayUtils.randomArray(ArrayUtils.java:48)
at ArrayUtilsTester.main(ArrayUtilsTester.java:9)

Help?
 
Encephalopathic
Posts:13,714
Registered: 12/20/07
Re: Java problems....again (Creating random array + its values)      
Nov 7, 2009 4:01 PM (reply 1 of 5)  (In reply to original post )

 
what if you change
for (int i=0;i<=array2.length;i++)

to
for (int i = 0; i < array2.length; i++)

Does it make sense why this is important?
 
A_A_M_I_R
Posts:52
Registered: 2/21/09
Re: Java problems....again (Creating random array + its values)   
Nov 7, 2009 4:10 PM (reply 2 of 5)  (In reply to #1 )

 
Thanks yeah I get it now. If the array length is 5, it already loops through 5 and then it should stop. Was hoping you could help me out with more parts of this exercise. Will post them in 15 mins.

Edited by: A_A_M_I_R on Nov 8, 2009 12:10 AM
 
njb7ty
Posts:1,153
Registered: 2/7/08
Re: Java problems....again (Creating random array + its values)      
Nov 7, 2009 4:12 PM (reply 3 of 5)  (In reply to #2 )

 
If you fill up your code with System.out.println() statements, you will be able to see for yourself if it generates random values. It will probably also answer many other questions you may have. (eventually, you should learn to use the debugger instead of relying on System.out.println() ).
 
A_A_M_I_R
Posts:52
Registered: 2/21/09
Re: Java problems....again (Creating random array + its values)   
Nov 7, 2009 7:35 PM (reply 4 of 5)  (In reply to #3 )

 
Yup, thanks to all i'm done =D
 
punter
Posts:116
Registered: 9/8/09
Re: Java problems....again (Creating random array + its values)   
Nov 8, 2009 8:01 PM (reply 5 of 5)  (In reply to #3 )

 
njb7ty wrote:
If you fill up your code with System.out.println() statements, you will be able to see for yourself if it generates random values. It will probably also answer many other questions you may have. (eventually, you should learn to use the debugger instead of relying on System.out.println() ).

Would :

1. Using a debugger
2. Using Debugging Sysouts
3. A ponder after forming the correct question to be put in the forum

actually providing a helping hand in solving the issue ?

Thanks.
 
This topic has 5 replies on 1 page.
Back to Forum
 
Read the Developer Forums Code of Conduct

Click to email this message Email this Topic

Edit this Topic
  
 
 
Forums Statistics
    Users Online : 54
  • Guests : 135

About Sun forums
  • Sun Forums is a large collection of user generated discussions. It is here to help you ask questions, find answers, and participate in discussions.

    Check out our guide on Getting started with Sun Forums for a full walkthrough of how to best leverage the benefits of this community.

Powered by Jive Forums