skip to Main Content

How can I return 1 value, in case I have some IF statements?

For example, when I choose Shopify, it calls a new method that allows me to type into console some need credentials and then append all typied data into 1 String and return that String(test) to Main class.
In case, I choose Bigcommerce, it calls pretty much the same method and append all needed credentials for Biggcomerce into 1 String(test2) as well. But in this case public String credentailsCollector() should return test2.

How can I do that? Thanks.

public class CartCredentialsCollector {
//it should return something like this: 
//store_url=https://test.myshopify.com&apiKey=myapikey&apiPassword=myapipassword

public String credentailsCollector() throws IOException {

    BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in));

    System.out.println("Do you want to connect a new shopping cart ('yes'/'no')");
    String newConnection = bufferedReader.readLine();
    if (newConnection.equals("no")) {
        bufferedReader.close();
    } else {
        System.out.println("Specify your shopping cart. It works with Shopify and Bigcommerce only");
        String newShoppingCart = bufferedReader.readLine();
        if (newShoppingCart.equals("Shopify")) {
            ShopifyCredentialsHandler sch = new ShopifyCredentialsHandler();
            String test = sch.shopifyCredentialsHandler();
        }
        else if (newShoppingCart.equals("Bigcommerce")) {
        BigcommerceCredentialsHandler bch = new BigcommerceCredentialsHandler();
        String test2 = bch.bigcommerceCredentialsHandler()
        }

        else {
            System.out.println("This method works with Shopify and Bigcommerce. Try to connect a Shopify or Bigcommerce store.");
            bufferedReader.close();
        }
    }
    // Here I need to return test (in case I choose "Shopify") or test2 (in case I choose "Bigcommerce"). How can I do that?
    }
}

3

Answers


  1. You should either declare test before your if statements or make multiple returns – one inside each if.

    Login or Signup to reply.
  2. You can use a method scope String variable and set the value of that instead of two different String variables. Something like this:

    public class CartCredentialsCollector {
    //it should return something like this: 
    //store_url=https://test.myshopify.com&apiKey=myapikey&apiPassword=myapipassword
    
    public String credentailsCollector() throws IOException {
        String test = null;
        BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in));
    
        System.out.println("Do you want to connect a new shopping cart ('yes'/'no')");
        String newConnection = bufferedReader.readLine();
        if (newConnection.equals("no")) {
            bufferedReader.close();
        } else {
            System.out.println("Specify your shopping cart. It works with Shopify and Bigcommerce only");
            String newShoppingCart = bufferedReader.readLine();
            if (newShoppingCart.equals("Shopify")) {
                ShopifyCredentialsHandler sch = new ShopifyCredentialsHandler();
                test = sch.shopifyCredentialsHandler();
            }
            else if (newShoppingCart.equals("Bigcommerce")) {
            BigcommerceCredentialsHandler bch = new BigcommerceCredentialsHandler();
            test = bch.bigcommerceCredentialsHandler()
            }
    
            else {
                System.out.println("This method works with Shopify. Try to connect a Shopify store.");
                bufferedReader.close();
            }
        }
        return test;
        // Here I need to return test (in case I choose "Shopify") or test2 (in case I choose "Bigcommerce"). How can I do that?
        }
    }
    
    Login or Signup to reply.
  3. As you have realised, the reason you can’t access the “test” or “test2” variables where you want to make your return is because both of them are out of scope at that point.

    You need to either declare a String variable up top (before your first if statement) where the scope of the variable would extend through the entire credentialsCollector() method, or use multiple returns, putting each return in the same scope as your “test” and “test2” variables.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search