Results 1 to 5 of 5
  1. #1
    FOX427 is offline Member
    Join Date
    Jun 2011
    Posts
    54
    Rep Power
    0

    Default Arrays, filling an array with set of values

    Hi All!

    I am having little issue with my code here:

    Java Code:
    /**
     * This program fills an array "squares" with the values to be squared at the end, 
    for example, output should be  1 4 9 16 25 36 49 64 81 100
     * @author FOX
     *
     */
    
    public class R7d7 {
    
    	/**
    	 * @param args
    	 */
    	private int i;
    	
    	public void fillSquare()
    	{
    		int[] squares = new int[10];
    		
    		for (int i = 0; i<squares.length; i++);
    		{
    			squares[i]=i*i;
    			System.out.println(squares[i]);
    		}
    				
    	}
    	public static void main(String[] args) {
    		// TODO Auto-generated method stub
    		R7d7 test = new R7d7();
    		
    		test.fillSquare();
    
    	}
    
    }
    the program gives me a single value(that is 0) instead of giving a list of squares. I don't know where I got wrong but will be very thankful if someone helps me, thanks!
    Last edited by FOX427; 12-30-2012 at 08:41 PM. Reason: wrong comments

  2. #2
    eRaaaa is offline Senior Member
    Join Date
    Oct 2010
    Location
    Germany
    Posts
    787
    Rep Power
    6

    Default Re: Arrays, filling an array with set of values

    for (int i = 0; i<squares.length; i++); <- you have to delete this semikolon!
    Otherwise your loop looks similar to
    Java Code:
            for (int i = 0; i<squares.length; i++)
            {
                
            }
    ->
    Java Code:
            for (int i = 0; i<squares.length; i++)
            {
                
            }
            {
                squares[i]=i*i;
                System.out.println(squares[i]);
            }
    no printing within loop, one single print after loop!

  3. #3
    FOX427 is offline Member
    Join Date
    Jun 2011
    Posts
    54
    Rep Power
    0

    Default Re: Arrays, filling an array with set of values

    haha! can't believe that I made such an elementary mistake! thanks you ! :))

  4. #4
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,651
    Blog Entries
    7
    Rep Power
    21

    Default Re: Arrays, filling an array with set of values

    It's completely off topic but I like the comments in your code (they're totally incorrect); better remove your comments instead of leaving in incorrect comments.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  5. #5
    FOX427 is offline Member
    Join Date
    Jun 2011
    Posts
    54
    Rep Power
    0

Similar Threads

  1. Filling array with 10 random values
    By quinnvanorder in forum New To Java
    Replies: 6
    Last Post: 05-26-2012, 09:03 PM
  2. Issue with filling array full of random values
    By quinnvanorder in forum New To Java
    Replies: 5
    Last Post: 05-20-2012, 01:04 AM
  3. Replies: 0
    Last Post: 10-10-2011, 06:01 PM
  4. Filling Arrays??
    By Bgreen7887 in forum New To Java
    Replies: 8
    Last Post: 11-04-2010, 06:51 AM
  5. 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

Tags for this Thread

Posting Permissions

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