-1

When I am halfway through a method I would like to be able to go back to a certain point in it, somewhat like a checkpoint.

When running the code there would be two options: writing either 1 or 2. If 1 is the input I just want it to continue with something else, but if 2 is the input I want it to check if a boolean is true. If that boolean is true, ask again to choose 1 or 2. If the boolean is false, continue with more code.

boolean temp = true;

I would want it to come back to here

Scanner input = new Scanner(System.in);
int choice = input.nextLine;

if(choice==1) 
  System.out.println("Continue code");
else if(choice==2) {
  if(temp) {

Here I want it to come back to the start

  }
  else {
  System.out.println("Continue code");
  }
}

I can imagine there might be a function to do this, but I have no idea what it could be. This is for an actual program with more complex stuff, where a while or do while loop would not be ideal, but if it is the only way then I would also appreciate being told how that would work.

  • Use a loop and a `continue` statement – GBlodgett Jan 12 '19 at 22:22
  • @GBlodgett What is a `continue` statement? And as mentioned in the question I would prefer not to use a loop. –  Jan 12 '19 at 22:24
  • Why don't you want to use a loop? It would be the easiest way to accomplish this. (And the [`continue`](https://stackoverflow.com/questions/389741/what-is-the-continue-keyword-and-how-does-it-work-in-java) statement) – GBlodgett Jan 12 '19 at 22:27
  • @GBlodgett I have many loops in my code already, so if I use the `continue` statement how would I know what loop it refers to? Also, how would I use a loop? –  Jan 12 '19 at 22:31
  • You can use [labeled loops](https://stackoverflow.com/questions/3821827/loop-in-java-code-what-is-this-and-why-does-it-compile) – GBlodgett Jan 12 '19 at 22:32
  • @GBlodgett Is there a `break` statement in java? In the link you referred to it said the difference between a `break` and a `continue` was that the `continue` skipped the rest of the code in the loop but continued the loop. Also, how would I use loops and `continue` to achieve this? Would you be able to submit an answer? –  Jan 12 '19 at 22:38
  • It might be cleaner to use recursion: ```void foo() { .... if (startOver) { foo(); return; }``` –  Jan 12 '19 at 22:42
  • 1
    Or possibly just break the code into multiple methods. The note that "this is for an actual program with more complex stuff" suggests that it could be suffering from hugemethoditis. Though, on the whole, it seems fairly obvious that going back to the beginning *is* a loop and therefore there *should be* an actual loop statement. –  Jan 12 '19 at 22:48

2 Answers2

1

You can do this by using recursive also, write recursive code inside a method and call that method when you like to loop

public void test(Scanner input, boolean temp) {

    int choice = input.nextInt();

    if (choice == 1)
        System.out.println("Continue code");
    else if (choice == 2) {
        if (temp) {
            test(input, temp);

        } else {
            System.out.println("Continue code");
        }
    }

}

Execution Code

public class DemoMain2 {

public static void main(String[] args) {

    Scanner sc = new Scanner(System.in);
    test(sc, true);

}

public static void test(Scanner input, boolean temp) {

    int choice = input.nextInt();

    if (choice == 1)
        System.out.println("Continue code");
    else if (choice == 2) {
        if (temp) {
            System.out.println("calling test method again, temp value is : "+temp);
            test(input, temp);
            // you can update temp value as required

        } else {
            System.out.println("Continue code");
            }
        }

    }
 }

Input & Output

2
calling test method again, temp value is : true
2
calling test method again, temp value is : true
2
calling test method again, temp value is : true
2
calling test method again, temp value is : true
1
Continue code

Since temp is always true and input is 2 test method is called from if block, if input is 1 then it will execute the else block

Ryuzaki L
  • 37,302
  • 12
  • 68
  • 98
  • What is recursive code? Also, when the `test` method is finished (the second time) wouldn't it continue with the first time it was running `test`? –  Jan 12 '19 at 22:43
  • the recursive the calling same method inside it, like in the `if` block calling the `test` method, do you have desired input and output so that it will be clear @NabilotexD – Ryuzaki L Jan 12 '19 at 22:46
  • Yes, if `temp` was true and `input` is 2 –  Jan 12 '19 at 22:48
0

Unlike some other languages, Java does not have "jumping" statements (like 'goto'). You must use a loop so you can then use "continue" or "break" statements with labels to refer what statement should run next.

boolean temp = true;

label:
while(true) {
        Scanner input = new Scanner(System.in);
        int choice = input.nextInt();

    if(choice==1) {
        System.out.println("Continue code");
        break;
    }
    else if(choice==2) {
        if(temp) {
            continue label;
        }
        else {
            System.out.println("Continue code");
            break;
        }
    }
    }se if(choice==2) {
if(temp) {
    continue label;
}
else {
    System.out.println("Continue code");
}

}

Notice that this isn't a good practice Using a nice statement in the loop condition would be way better:

boolean temp = true;

Scanner input = new Scanner(System.in);
int choice;
do{
    choice = input.nextInt();
    if(choice==1) {
        System.out.println("Continue code");
        temp=false;
    }
    else if(choice==2) {
        if(!temp){
            System.out.println("Continue code");
            temp=false;
        }
    }
}
while(choice==2 && temp);

Another option is a recursive call, but I wouldn't recommend that solution since it allocates lots of memory unnecessarily and expanding the call stack.

SomoKRoceS
  • 2,934
  • 2
  • 19
  • 30