Go4Expert

Go4Expert (http://www.go4expert.com/)
-   Java (http://www.go4expert.com/forums/java/)
-   -   Java arrays (http://www.go4expert.com/forums/java-arrays-t9558/)

cyrow 23Mar2008 19:25

Java arrays
 
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?


All times are GMT +5.5. The time now is 03:47.