Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

It prints Flip Method (only prints 9 to 5 ) but I want it to print from 9 to 0 http://imgur.com/gggiJwn public static void flip(int[] flp){ System.out.println("This is the flip method");

     for ( int i = 0;  i < flp.length; i++){
     int e = flp.length-1;
     int temp = flp[e-i];
     flp[i] = flp[e-i];
     flp[i] = temp;
      e--;
     System.out.println("Index"+(i)+" :"+flp[i]); //is this the problem?

   }

    }
}
share|improve this question

closed as off-topic by Jens, jww, biegleux, SilentKiller, MysticMagicϡ Oct 13 '14 at 5:55

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "Questions seeking debugging help ("why isn't this code working?") must include the desired behavior, a specific problem or error and the shortest code necessary to reproduce it in the question itself. Questions without a clear problem statement are not useful to other readers. See: How to create a Minimal, Complete, and Verifiable example." – Jens, jww, biegleux, SilentKiller, MysticMagicϡ
If this question can be reworded to fit the rules in the help center, please edit the question.

1  
Please provide a runable example –  Jens Oct 13 '14 at 5:19
    
Please provide a coherent question. –  James Oct 13 '14 at 5:24
    
imgur.com/gggiJwn // this is with for ( int i = 0; i <flp.length; i++) –  Codecka Oct 13 '14 at 5:25
    
You really shouldn't change the question once people start answering. If you have a new question, then ask a new question. –  David Wallace Oct 13 '14 at 5:47

6 Answers 6

up vote 0 down vote accepted

You need to change your logic to following to reverse the full array

 public static void flip(int[] flp){
 System.out.println("This is the flip method");
 for ( int i = 0;  i < flp.length/2; i++){
     int e = flp.length-(1+i);
     int temp = flp[i];
     flp[i] = flp[e];
     flp[e] = temp;
    // remove print from here. else you will get half of the array
    // since flp.length/2
   }
}

Add that in a separate method.

 for(int i = 0;  i < flp.length; i++){
   System.out.println("Index"+(i)+" :"+flp[i]);
 }
share|improve this answer
    
but when i do i < flp.length; it prints from 9 to 5, then 5 to 9 –  Codecka Oct 13 '14 at 5:21
    
@Codecka see my edit. –  Ruchira Gayan Ranaweera Oct 13 '14 at 5:31
    
imgur.com/WxYvaFI //im still having the issue –  Codecka Oct 13 '14 at 5:37
    
@Codecka You have to change your first for loop to my one. Your reversing way is not correct. –  Ruchira Gayan Ranaweera Oct 13 '14 at 5:38
    
i did the adjustments like yours it prints the array from 0 - 9... i want it to print from 9 to 0.. –  Codecka Oct 13 '14 at 5:48

The problem is that you are printing half of the elements

i < flp.length/2

That should be

i < flp.length
share|improve this answer
1  
+1, competing on time after a long time ;-) you won by 10 seconds. –  Juned Ahsan Oct 13 '14 at 5:19
    
but when i do i < flp.length; it prints from 9 to 5, then 5 to 9 :/ –  Codecka Oct 13 '14 at 5:19
    
By changing i < flp.length/2 will change the reversing logic. to print the array should use different for loop. –  Ruchira Gayan Ranaweera Oct 13 '14 at 5:32

Actually, your for loop is fine - but you should remove the printing from inside that loop, and have it in a seperate loop after the flip loop

 for ( int i = 0;  i < flp.length; i++){
   System.out.println("Index"+(i)+" :"+flp[i]);
 }
share|improve this answer

Your method should be like this,as you are replacing the elements you have to loop through the half of the size of array.

  public static void flip(int[] flp){
        System.out.println("This is the flip method");
        int e = flp.length-1;
        for ( int i = 0;  i < flp.length/2; i++){
            int temp = flp[e];
            flp[e] = flp[i];
            flp[i] = temp;
            e--;
      }
      System.out.println(Arrays.toString(flp));
   }

OUTPUT

[9, 8, 7, 6, 5, 4, 3, 2, 1, 0]
share|improve this answer
public static void flip(int[] flp) {
    System.out.println("This is the flip method");

    int e = flp.length - 1;
    for (int i = 0; i < flp.length / 2; i++) {

        int temp = flp[i];
        flp[i] = flp[e];
        flp[e] = temp;
        e--;

    }

    for (int i = 0; i < flp.length; i++) {
        System.out.println("Index" + (i) + " :" + flp[i]); 
    }
}
share|improve this answer

Your for loop prints 9 to 5 because this is how you have configured your loop to iterate till i < flp.length/2 .

Change it to:

i < flp.length

from

i < flp.length/2
share|improve this answer

Not the answer you're looking for? Browse other questions tagged or ask your own question.