Results 1 to 6 of 6
  1. #1
    quinnvanorder is offline Member
    Join Date
    Feb 2012
    Posts
    31
    Rep Power
    0

    Default Issue with filling array full of random values

    I feel like a silly, as I have done this before and it worked fine, but I am trying to fill an array with 10 random values so that I can implement a selection sort on it. I have all my code as I think it should be, but by placing various test lines in my code, it appears that it is not actually putting the values in the array. Am I missing something obvious, or am I just plain doing it wrong?

    I am attempting to use a for loop to count through the array and place a value between 1 and 50 in it, however a S.O.P command in the for loop is not outputting anything. Furthermore, when I run the code, it only outputs "test" 10 times to console, implying that it is successfully calling the selection sort method, while not successfully calling the output for loop directly below the call to the selection sort.

    Java Code:
    import java.util.Random;
    
    public class J2_Lab_7
    	{	static int[] num = new int[10];
    		static Random generator = new Random();
    		public static void main (String[] args)
    			{	for (int i=0;i>num.length;i++)
    					{	num[i]=generator.nextInt(50)+1;
    						System.out.println(num[i]); //this should be outputing the values of num[] but it is not??
    					}
    				SelectionSort(num);
    				for (int j=0; j>num.length; j++)
    					{	System.out.println (num[j]);
    						System.out.println("test2");//this is not being called successfully
    					}
    			}
    	
    		public static void SelectionSort(int[] number)
    			{	int i, j, first, temp;
    				for (i = number.length-1; i>0; i--)  
    					{	first = 0;   //initialize to subscript of first element
    						for(j = 1; j<=i; j++)   //locate smallest element between positions 1 and i.
    							{	if( number[j] < number[first] )         
    								first = j;
    							}
    						temp = number[first];   //swap smallest found with element in position i.
    						number[first] = number[i];
    						number[i] = temp; 
    						System.out.println("test");//this is being called successfully
    					}           
    			}
    	}

  2. #2
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,560
    Rep Power
    25

    Default Re: Issue with filling array full of random values

    S.O.P command in the for loop is not outputting anything
    If the for loops are not being executed, there must be something wrong with the way they are coded. Check that they are coded correctly.
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    quinnvanorder is offline Member
    Join Date
    Feb 2012
    Posts
    31
    Rep Power
    0

    Default Re: Issue with filling array full of random values

    As I understand for loops it is correct, I have my implementation, my test and my modifier, and they are calling an array that does exist. Furthermore, It is not getting any errors

    Also just to be sure, I put a test S.O.P line in one of the for loops in the SelectionSort method, and it worked fine, and it is structured identically to the for loop that appears to not be working.

  4. #4
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,560
    Rep Power
    25

    Default Re: Issue with filling array full of random values

    Print out the value of the expression that is used to control the execution of the for loop. It needs to be true for the loop to execute.
    If you don't understand my response, don't ignore it, ask a question.

  5. #5
    quinnvanorder is offline Member
    Join Date
    Feb 2012
    Posts
    31
    Rep Power
    0

    Default Re: Issue with filling array full of random values

    I am not sure what you mean, it is just a for loop that starts with i at zero and goes to the length of the array num[] to start with, the array is set to ten values long, and with no data in it when it is instantiated, and it is meant to fill the array with values, it is just not working.

  6. #6
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,560
    Rep Power
    25

    Default Re: Issue with filling array full of random values

    it is just not working.
    Print out the value of the expression in the for loop that controls when the loop executes. You called it:
    my implementation, my test and my modifier
    If you don't understand my response, don't ignore it, ask a question.

Similar Threads

  1. Issue with setting values in an array
    By pyat77 in forum New To Java
    Replies: 4
    Last Post: 11-04-2011, 02:01 AM
  2. Algorithm for filling random polygons
    By theraccoon in forum Java 2D
    Replies: 3
    Last Post: 05-18-2011, 06:35 PM
  3. problem filling list with values from CSV feed
    By shy_ted in forum New To Java
    Replies: 7
    Last Post: 10-12-2010, 10:19 AM
  4. Replies: 6
    Last Post: 08-26-2010, 02:26 PM
  5. Filling 2D Array
    By Nakira in forum New To Java
    Replies: 3
    Last Post: 11-12-2008, 12:43 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •