1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Java arrays

Discussion in 'Java' started by cyrow, Mar 23, 2008.

  1. cyrow

    cyrow New Member

    Joined:
    Nov 19, 2007
    Messages:
    21
    Likes Received:
    0
    Trophy Points:
    0
    Code:
    class Test{
    	public static void main(String[] args){
    		int [] num = {6,4,3,5,7};
    	
    	for(int j = 0; j < num.length; j++){
    			for(int i = 0; i < num.length-1; i++){
    				if(num[i]+num[i+j]==10){
    					System.out.println(num[i]);
    					System.out.println(num[i+j]);
    				}
    			 }
    		}
    	}//end main
    }//end class
    
    //output when printed above
    5
    5
    6
    4
    3
    7
    //error
    Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 5
    at Test.main(Test.java:14)

    Problem
    I am trying to go through the array and print all the two digits which add up to 10. In the answer above show 5, but 5 should be part of the answer, what might the problem be?
     
    Last edited by a moderator: Mar 24, 2008

Share This Page