Results 1 to 7 of 7
Like Tree1Likes
  • 1 Post By Fubarable

Thread: Filling array with 10 random values

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

    Default Filling array with 10 random values

    For the life of me I cannot understand why this code does not fill the array with random values. I would really appreciate any help.

    Java Code:
    import java.util.Random;
    
    public class test
    	{	public static void main (String[] args)
    			{	int[] num = new int[10];
    				Random generator = new Random();
    				for (int i=0;i>10;i++)
    					{	num[i]=generator.nextInt(50)+1;
    						System.out.println(num[i]); //this should be outputing the values of num[] but it is not??
    					}
    			}
    	}

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

  3. #3
    romero4742 is offline Member
    Join Date
    Mar 2012
    Posts
    29
    Rep Power
    0

    Default Re: Filling array with 10 random values

    Quote Originally Posted by quinnvanorder View Post
    For the life of me I cannot understand why this code does not fill the array with random values. I would really appreciate any help.

    Java Code:
    for (int i=0;i>10;i++)
    Just to spare your life haha the for loop you got the < backwards you are telling it to continue until i is not > 10 but i = 0 is not greater than 10 so it never starts.

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

    Default Re: Filling array with 10 random values

    I originally had it as i>num.length, and it was not working, thus I changed it to i>10 and it is still not working :(

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

    Default Re: Filling array with 10 random values

    Quote Originally Posted by romero4742 View Post
    Just to spare your life haha the for loop you got the < backwards you are telling it to continue until i is not > 10 but i = 0 is not greater than 10 so it never starts.
    oh... of course *smacks forehead* thanks man

  6. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default Re: Filling array with 10 random values

    romero stated literally what I was trying to get you to discover on your own...

  7. #7
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default Re: Filling array with 10 random values

    Also, you should fix your code formatting. Don't start code on the same line as an opening bracket, and use a little more whitespace to improve readability. Also, you will want to follow Java naming rules with your class names starting with capital letters and method and variable names with lower-case letters. So for instance, your code may want to look like...

    Java Code:
    import java.util.Random;
    
    public class Test {
       public static void main(String[] args) {
          int[] num = new int[10];
          Random generator = new Random();
          for (int i = 0; i < 10; i++) {
             num[i] = generator.nextInt(50) + 1;
             System.out.println(num[i]); 
          }
       }
    }
    This may seem trivial, and perhaps with your small program it is, but it becomes very important when you are trying to get others to understand larger more complex programs. So it's better to start good habits now.
    DarrylBurke likes this.

Similar Threads

  1. Issue with filling array full of random values
    By quinnvanorder in forum New To Java
    Replies: 5
    Last Post: 05-20-2012, 02:04 AM
  2. Algorithm for filling random polygons
    By theraccoon in forum Java 2D
    Replies: 3
    Last Post: 05-18-2011, 07: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, 11:19 AM
  4. Replies: 6
    Last Post: 08-26-2010, 03:26 PM
  5. Filling 2D Array
    By Nakira in forum New To Java
    Replies: 3
    Last Post: 11-12-2008, 01: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
  •