0

I have two classes named as Test1 and Test2.

class Test1{
    public void exe1(){
        System.out.println("Execute only one time");
        System.out.println("Execute iterations");
        System.out.println("Execute only last time");
    }
}

class Test2{
    public statuc void main(String args[]){
        Test1 ts = new Test1();
        for(int i=0 ; i<=3;i++){
            ts.exe1();
        }
    }
}

The ouput of this result would be :

Execute only one time
Execute iterations
Execute only last time
Execute only one time
Execute iterations
Execute only last time
Execute only one time
Execute iterations
Execute only last time

Is it possible in java that the output that i get should be :

Execute only one time
Execute iterations
Execute iterations
Execute iterations
Execute only last time

I mean the 1st print statement should execute at very 1st time and then number of iterations may print and at the end my last print statement should execute.

5
  • Use switch or if statement Commented May 12, 2015 at 8:43
  • 7
    83 previous questions and you're still posting nearly-unformatted code?! Well, that shows respect for the community you're asking to help you. Commented May 12, 2015 at 8:44
  • You might need to pass in the current and total numbers of iterations to exe1 so that it can know whether it is the first, last or other iteration. Commented May 12, 2015 at 8:45
  • 1
    The code doesn't so what you claim, it executes the loop 4 times. Commented May 12, 2015 at 8:52
  • and in java there is nothing like "statuc" and what you have printed in output will not match your code Commented May 12, 2015 at 8:59

4 Answers 4

3
public void exe1(int i){
    if(i==0) System.out.println("Execute only one time");
    if(i==3) System.out.println("Execute only last time");
    else System.out.println("Execute iterations");
}

for(int i=0 ; i<=3;i++){
    ts.exe1(i);
}

Use i in your loop as a function argument.

Sign up to request clarification or add additional context in comments.

5 Comments

I would pass in 3 also, so it isn't a magic number scattered through the code. Also, by the spec in the question, the first and last messages should be printed in addition, not instead of.
@Alex :- Instead of taking hard coded value as 3 , if i'm taking a list.size() then how can it executed ? like for(int i=0;i==ls.size();i++) where ls is an instance arryalist and having 3 objects added to it.
@Littlebird if you have list with three objects and use "for(int i=0;i==ls.size();i++)", it will be iterate 4 times and not three, you must use for(int i=0;i<ls.size();i++) see my comment stackoverflow.com/a/30186059/4580058
@Littlebird In this case just replace "3" with "ls.size()" in both loop initiation and conditional statement
@AlexShutin: I tried it but working . I created another post can you please look into my code where i'm wrong ? stackoverflow.com/questions/30190753/…
0

Try to use two parameters to check if the text should be printed or not

public void exe1(int counter, int totalCount){
   if (counter == 0) System.out.println("Execute only one time");
   System.out.println("Execute iterations");
   if (counter == totalCount) System.out.println("Execute only last time");
}

for(int i=0 ; i<=3;i++){
   ts.exe1(i,3);
}

2 Comments

This code will print "Execute iterations" 5 times instead of 3.
only 4 times :) then you must adjust the iteration code to i<=2, while the number of iterations is 4
0
class Test1 {
public void exe1(boolean isFirst, boolean isLast) {

    if (isFirst) {
        System.out.println("Execute only one time");
    } else {
        if (isLast) {
            System.out.println("Execute only last time");
        } else {
            System.out.println("Execute iterations");
        }
    }
}
}

public class Test2 {
public static void main(String args[]) {

    Test1 ts = new Test1();

    for (int i = 0; i <= 3; i++) {

        if (i == 0) {
            ts.exe1(true, false);
        } else {

            if (i == 3) {
                ts.exe1(false, true);
            } else {
                ts.exe1(false, false);
            }
        }
    }
}
}

1 Comment

The output is not right. Iterations don't execute first and last time
0
  class Test1{
      public void exe1(int i){
          If(i==0)
              System.out.println("Execute only one time");
          If(i>0 && i<3)
              System.out.println("Execute iterations");
          Else
              System.out.println("Execute only last time");

    }
}

By the way, next time please format your code in an adequate manner and check for mistakes. If you ran that as it is, it wouldn't compile.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.