-4

What I need is a way to go back to the top of the method is a special event is triggered, here's my code in C#

public void SendRequest()
{

    send:
    {

        var response = new RequestBuilder("https://google.com").
            WithMethod(HttpMethod.GET).
            Send();

        if (response.Contains("Special Keyword"))
            goto send;

        // Continue..

    }

}

This code works in C#, it resend the request until the page doesn't contains "Special Keyword"

Any idea of how I can do the same thing in Java?

Here's what I tried, but even if the page contains "Special Keyword" it ends up saying "Done"

public void SendRequest() {

    send: {

        String response = new RequestBuilder().
                to("https://google.com").
                buildAndSend().
                toString();

        if(response.contains("Special Keyword"))
            break send;

        // Continue..

        System.out.println("Done");

    }

}
Peter Wave
  • 11
  • 3

4 Answers4

3

With a loop:

while (true) {
    String response = ...; 
    if (response.contains("Special Keyword")) {
      continue; // takes you back to the start of the loop.
    }

    break;
}

Honestly, I would scratch my head if I saw this in real code. It would be more idiomatic to invert the condition:

while (true) {
  String response = ...; 
  if (!response.contains("Special Keyword")) {
    break;
  }
  // No continue.
}
Andy Turner
  • 137,514
  • 11
  • 162
  • 243
  • Wouldn't this be better? `do { ... } while (response.Contains("Special Keyword"));` – Andreas Nov 16 '18 at 21:12
  • @Andreas you can do that, if you're OK with/need response outside the scope of the loop. I was trying to keep it similar to the question code. – Andy Turner Nov 16 '18 at 21:16
2

You can use a while loop. A do-while loop seems appropriate in your case:

String response;

do {
    response = new RequestBuilder().
            to("https://google.com").
            buildAndSend().
            toString();
} while (response.contains("Special Keyword"));


System.out.println("Done");
Andy Turner
  • 137,514
  • 11
  • 162
  • 243
ernest_k
  • 44,416
  • 5
  • 53
  • 99
  • 1
    Note that using a do/while loop necessarily requires the response variable to be declared outside the loop, which may not be desirable if you don't use it afterwards. Also note that you can leave response initially unassigned, before the loop, since it is always assigned in the loop before it is read. – Andy Turner Nov 16 '18 at 19:34
0

goto is a reserved work in java, but not used.

In fact, goto is a bad praticy Why is goto Bad

cvdr
  • 939
  • 1
  • 11
  • 18
0

Function calls are a way of replacing goto. So you could just do a recursive call to the function like this (but translate it to Java):

public void SendRequest()
{
    var response = "Testing123...";

    // Form the request here...

    if (response.Contains("Special Keyword"))
    {           
        SendRequest();
    }

}
exiled1
  • 16
  • 1