Results 1 to 4 of 4

Thread: New to Java.

  1. #1
    xbeto is offline Member
    Join Date
    Feb 2012
    Posts
    3
    Rep Power
    0

    Question New to Java.

    Hi...

    I'm learning Java and I have an issue with this code... I'm using eclipse... I hope someone can help me to understand where is my mistake...

    Java Code:
    public class arrays22 {
    	public static void main (String[]args){
    	
    		int[] nums = new int[10];
    		
    		
    	asignar(nums);
    	ordenar(nums);
    	mostrar(nums);
    	
    	}
    	
    	public static void asignar(int[]m){
    		int c = 0;
    		for(int i=0;i<=m.length;i++){
    		m[i]=c;
    		c++;
    		}
    	}
    	
    	public static void ordenar(int[]m){
    		int aux;
    		for (int i = 0;i<m.length;i++){
    			
    			for(int j=i+1;j<m.length;j++){
    				if(m[j]<m[i]){
    					aux=m[i];
    					m[i]=m[j];
    					m[j]=aux;
    					}
    			}
    				
    			
    			
    		}
    	}
    	
    	public static void mostrar(int[]m){
    		for  (int num:m){
    			System.out.println(num);
    			
    		}
    	}
    	
    	
    }
    Eclipse does not show any error in the code but the code does not run... the erros displayed by eclipse is:

    Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 10
    at arrays22.asignar(arrays22.java:17)
    at arrays22.main(arrays22.java:8)

  2. #2
    xbeto is offline Member
    Join Date
    Feb 2012
    Posts
    3
    Rep Power
    0

    Default Re: New to Java.

    I make it works =)

  3. #3
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default Re: New to Java.

    Java Code:
    Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 10
    at arrays22.asignar(arrays22.java:17)
    at arrays22.main(arrays22.java:8)
    The message is saying that the index 10 is too big.

    Java Code:
    public static void asignar(int[]m){
        int c = 0;
        for(int i=0;i<=m.length;i++){
            m[i]=c;
            // etc
    The ten array elements go from 0 to 9. So you should say i<m.length.

    [Edit] too slow ;( - I'm glad you've got it figured out

  4. #4
    xbeto is offline Member
    Join Date
    Feb 2012
    Posts
    3
    Rep Power
    0

    Default Re: New to Java.

    Thanks pbrockway2, I have modified it and now it's working, now I can continue learning.. thanks again...

Posting Permissions

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