Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 8 of 8

Thread: Printing an array

  1. #1
    Junior Member
    Join Date
    Jul 2011
    Posts
    9
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Printing an array

    Hello all! My first post here

    I've started teaching myself java and at the moment am making a really simple number sorter to sort an array in ascending order. As far as I'm aware I have the number sorting bit down and amusingly the thing I'm stuck on is getting the new sorted array to print. I'm trying to do it via loops rather than the prebuilt java classes as I really want to get a proper grasp of loops before i move on. Anyways I'm getting 15 compile errors at the mo (won't post them here as they are really long) and they are all for my print loop. If anyone could have a look at my code and tell me whats up it would be much appreciated!
    Thanks
    Native

    import java.io.*;
    class SortAsc
    {
    public static void main ( String agrs[] )
    	{
     	int A[]={8,9,3,10};
     	int i,k;
     	for(i=0;i<4;i++)
     		{ 
    		for(k=0;k<4;k++)
    			{
    			if (A[k]<A[i])
    				{
    				int temp;
    				temp = A[k];
    				A[k] = A[i];
    				A[i] = temp;
    				}
    			}
    		}
    	}
    	int l;
    	for(l=0;l<4;l++)
    		{
    		System.out.print(A[l]);
    		}
    }
    Last edited by native; July 16th, 2011 at 06:45 AM.


  2. #2
    Junior Member
    Join Date
    Jul 2011
    Posts
    9
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Printing an array

    Gonna reply to my own post, the print part appears to be inside the wrong bracket set! muppet error
    New code looks like this
    import java.io.*;
    class SortAsc
    {
    public static void main ( String agrs[] )
    	{
     	int A[]={8,9,3,10};
     	int i,k;
     	for(i=0;i<4;i++)
     		{ 
    		for(k=0;k<4;k++)
    			{
    			if (A[k]<A[i])
    				{
    				int temp;
    				temp = A[k];
    				A[k] = A[i];
    				A[i] = temp;
    				}
    			}
    		}
    		int l;
    		for(l=0;l<4;l++)
    		{
    			System.out.print(A[l]);
    		}
    	}
    }

  3. #3
    Forum old-timer
    Join Date
    Nov 2008
    Location
    Faversham, Kent, UK
    Posts
    472
    My Mood
    Mellow
    Thanks
    4
    Thanked 58 Times in 54 Posts

    Default Re: Printing an array

    So is it fixed, or do you still get errors?

    Incidentally, when looping through arrays, don't use 'magic numbers' to hard-code the length, use array.length. This way, you can't put in the wrong length, and if the length of the array changes, your code doesn't have to.

  4. The Following User Says Thank You to dlorde For This Useful Post:

    native (July 16th, 2011)

  5. #4
    Junior Member
    Join Date
    Jul 2011
    Posts
    9
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Printing an array

    Hi Dlorde,
    Yes it works now! When you say don't use magic numbers instead use array.length what do you mean? Could you possibly show me its use on the above code?
    Thanks
    native

  6. #5
    Forum old-timer
    Join Date
    Nov 2008
    Location
    Faversham, Kent, UK
    Posts
    472
    My Mood
    Mellow
    Thanks
    4
    Thanked 58 Times in 54 Posts

    Default Re: Printing an array

    A magic number is a numeric literal placed directly in your code, and should be avoided. Far better to use a well named variable initialized with that value. In your code, the magic numbers are the hard-coded loop end values (4). The correct way to iterate an array with a 'for' loop is to use the array length property, e.g:
    int[] array = { 1, 2, 3, 4 };
    ...
    for(i=0; i < array.length; i++) {
       ...

    Incidentally, Java Naming Conventions mandate that class names start with uppercase letters and that method and variable names start with lowercase letters. This means you easily distinguish class names from variable names in source code.

    Source code is for people to read, so make it readable.

  7. The Following User Says Thank You to dlorde For This Useful Post:

    native (July 16th, 2011)

  8. #6
    Junior Member
    Join Date
    Jul 2011
    Posts
    9
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Printing an array

    Oh right I see! Works a treat. Thank you.

    Here is full working code for anyone who may stumble across this topic in the future.

    import java.io.*;
    class SortAscEdit
    {
    public static void main ( String agrs[] )
    	{
     	int array[]={8,9,3,10};
     	int i,k;
     	for(i=0;i<array.length;i++)
     		{ 
    		for(k=0;k<array.length;k++)
    			{
    			if (array[k]<array[i])
    				{
    				int temp;
    				temp = array[k];
    				array[k] = array[i];
    				array[i] = temp;
    				}
    			}
    		}
    		int l;
    		for(l=0;l<array.length;l++)
    		{
    			System.out.println(array[l]);
    		}
    	}
    }

  9. #7
    Forum old-timer
    Join Date
    Nov 2008
    Location
    Faversham, Kent, UK
    Posts
    472
    My Mood
    Mellow
    Thanks
    4
    Thanked 58 Times in 54 Posts

    Default Re: Printing an array

    OK, but my use of the variable name 'array' was just a generic example - I wasn't suggesting you use it. Always use descriptive names that tell people what the role of the variable or method or class is. Use noun forms for classes and variables, and verbal forms for methods.

    Your array of numbers might be better called 'numbers' or 'numberArray', to make it clear what it contains, so the code is more understandable.

  10. #8
    Junior Member
    Join Date
    Jul 2011
    Posts
    9
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Printing an array

    I was having trouble compiling under a different name so I called the array 'array' just to be sure it would compile. Then after I worked out why I was having compile problems so I guess I could change it to anything now.
    Thanks

Similar Threads

  1. printing array recursively
    By kyros in forum Algorithms & Recursion
    Replies: 2
    Last Post: November 19th, 2010, 01:04 AM
  2. Printing boolean values from an array
    By Deprogrammer in forum What's Wrong With My Code?
    Replies: 3
    Last Post: November 18th, 2010, 12:11 AM
  3. Printing the Max and Min in an Array
    By bonbon242 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: October 29th, 2010, 08:28 AM
  4. Doubt Regarding Printing an Array List
    By reeceypp in forum What's Wrong With My Code?
    Replies: 5
    Last Post: April 20th, 2010, 03:11 AM
  5. [SOLVED] Printing Array without printing empty elements
    By CarlMartin10 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: April 12th, 2010, 02:41 AM

Tags for this Thread