-1

Hi =) I have a question in my coding and I have no idea on it so I make a similar coding post here for help.

this is my coding:

package test;

public class Test {

public static void main(String[] args) {
    int i=0, j=5;
    while(i<10){
        skip(i,j);
        System.out.println(i);
        i++;
    }
}

public static void skip(int i, int j){
    if(i==j){
        i+=1;
    }
}
}

My problem is how can I skip the integer 5 with the call function skip?

my outcome should be like this: ("5" did't print out)

0
1
2
3
4
6
7
8
9
  • 2
    `if (i == 5) { continue; }` - don't forget to increment `i` also. – August Nov 11 '14 at 04:42
  • 1
    You just need if(i!=5){ – Juned Ahsan Nov 11 '14 at 04:43
  • You can't. Java is strictly [pass-by-value](http://stackoverflow.com/questions/40480/is-java-pass-by-reference-or-pass-by-value), so nothing you do in your `skip()` method is going to affect the loop counter in the calling code. – azurefrog Nov 11 '14 at 04:43
  • hey dude, I mean using call function...... this is just my similar coding, i not prefer use continue..... if really use continue also can't cause the output is 0 to 4 oni and 6 to 9 is gone.... – user3832964 Nov 11 '14 at 04:45

5 Answers5

2

You can't modify the caller's reference to a primitive value. They have no reference, and the wrapper type Integer is immutable. I think you're looking for something like

public static void main(String[] args) {
    int i = 0;
    while (i < 10) {
        System.out.println(i);
        i = skip(i);
    }
}

public static int skip(int i) {
    return (i == 4) ? i + 2 : i + 1;
}

The above ternary could be expressed as

public static int skip(int i) {
    if (i == 4) return i + 2;
    return i + 1;
}

Edit

Or,

public static int skip(int i) {
    if (i + 1 == 5) return i + 2;
    return i + 1;
}
Elliott Frisch
  • 198,278
  • 20
  • 158
  • 249
1
if (i == 5) {
    i++;
    continue;
}

You need to increment i by one before continue, otherwise it will end up for infinite loop because i value is not changing after 5.

Update: Try to run this code. It is not printing abc, so it is going it infinite loop

public static void main(String[] args) {
    int i=0;
    while(i<10){
        if (i == 5) {
            System.out.println(i);
            //i++;
            continue;
        }
        System.out.println(i);
        i++;
    }
    System.out.println("abc");
}
Naman Gala
  • 4,670
  • 1
  • 21
  • 55
0

try

package test;

public class Test {

    public static void main(String[] args) {
        int i=0;
        while(i<10){
            i=skip(i);
         }
    }

    public static int skip(int i){
        if(i!=5){
            System.out.println(i);
        }
        return i+1;
    }
}

EDIT

package test;

public class Test {

    public static void main(String[] args) {
        int i=0;
        while(i<10){
            i=skip(i);
            System.out.println(i);
         }
    }

    public static int skip(int i){
        if(i==4){
           i +=1;
        }
        return i+1;
    }
}
R A Khan
  • 187
  • 4
  • 12
0

Try this then :)

if(i==5){
   i=skip(i);               
}

private static int skip(int i) {
    i+=1;
    return i;   
}
SonalPM
  • 1,317
  • 8
  • 17
0

Look at the below code.

package test;

public class Test {
    int i=0;
    public void skip(int x)
    {
        if(x==i)
        {
            i++;
        }
    }
    public static void main(String args[])
    {
        int j=5;
        Test t=new Test();
        while(t.i<10)
        {
            t.skip(j);
            System.out.println(t.i);
            t.i++;
        }
    }
}
Kumar
  • 3,782
  • 4
  • 39
  • 87