This site requires JavaScript, please enable it in your browser!
Greenfoot back
divinity
divinity wrote ...

2016/1/5

reversed numbers

divinity divinity

2016/1/5

#
hi all first to begin let me wish everyone a happy new year to all i am doing a program where i have to display reversed numbers. the program ask the enter 10 number, now the program seem to working fine except when it reach to the number 10 it gives an array out of bound index. i know that it is asking for a number that is more than what it is in the array but am seeing where it is getting the array out of bound but the problems is who do i fix it. here the is the codes that i have
System.out.println("please enter 10 numbers");
        
        for(i=1; i<=10; i++)
        {
            values [i]= userinput.nextInt(); (this line of codes here is where i am gettng the array out of boundindex
        } 
        int[]reversednum;
        reversednum = new int[10];
        for(i = (values.length - 1); i>=0; i--)
        {
            reversednum [reversednum.length - 1 - i] = values[i];
            System.out.println("reversednum[i");
in line 5 is where i am getting the array out of bound index any help would be appreciated. thanks in advance
danpost danpost

2016/1/5

#
In java, counting (like for arrays and List objects) begins at zero. So, for example, the tenth element is at position nine. Interestingly enough, your second loop (at line 9) uses more appropriate values for 'i' (going from nine to zero). Adjust line 3 so that the values go from zero to nine; or, more appropriately:
for (i=0; i<values.length; i++)
divinity divinity

2016/1/5

#
ok thank ah fix the problem well at least one of the three problem. it is more or less supposed to an all in one program. it was supposed to be an array reversed number where it ask the use to enter ten number and it should reversed the 10 numbers and out of the 10 numbers the program should be able to output odd and even numbers. but i am not getting either of the three. dont know how to fix it. any help will do. this is the codes that i used to get the reversed number, odd numbers and even numbers
int i, n=0, limit=0;
        
        int[]values = new int[10];
       
            System.out.println("please enter 10 numbers");
        
        for(i=0; i<=values.length; i++)
        {
            values [i]= userinput.nextInt();
        } 
        int[]reversednum;
        reversednum = new int[10];
        for(i = (values.length - 1); i>=0; i--)
        {
            reversednum [reversednum.length - 1 - i] = values[i];
            System.out.println("reversednum[i");
        }  
             
        for(i =1; i<=limit; i++)
        {
            if(i %2 !=0)
            {
                System.out.println(i+" ");
            }
        }
        for(i=1; i<=limit; i++)
        {
            if(i %2 == 0)
            {
              System.out.println(i+"");
            }
        }
            
        
danpost danpost

2016/1/5

#
Line 16 will only print out the literal String value; it is not coded to print any value from the 'reversednum' array. Lines 23 an 30 will only print the index values ("1 3 5 7 9 0 2 4 6 8 ") since no value from either array is coded to print on those lines.
divinity divinity

2016/1/5

#
okay so it i back to the drawing boad for me, how do i get it to do what i want it to do
danpost danpost

2016/1/5

#
divinity wrote...
okay so it i back to the drawing boad for me, how do i get it to do what i want it to do
Why back to the drawing board? ... just change the parameters of the 'System.out.println' commands to print what you need them to.
You need to login to post a reply.