1

I'm trying to return the value of ?,?,? when executing the main function. However, I only get ?, as an answer. I don't want to use System.out.println (although it does the job) because I want to return the values to another function. The first return works as I want it to but in the second part, I am not sure how to concatenate the 2 returns and the for loop because useless after I've change the println with return

public static void main(String[] args)
{
  Scanner kb = new Scanner(System.in);
        r r = new r();
        System.out.println(r.func(3));
}

  public static String func(int size)
  {
    if(size == 1)
    return "?";
    else
    {
    for (int i = 1; i < size; i++)
    {
     return "?,";
      }
 return "?";
    }
  }
4
  • What do you think return does? Why do you think that your output would be ?,?,?. You mentioned concatenation in the title but I don't see any anywhere in your code where strings are being concatenated? Commented Oct 31, 2015 at 2:07
  • 1
    One and only one return statement will ever be executed... the for loop won't keep executing after the return if that is what you expect. You could write a recursive function to accomplish what you want though. Commented Oct 31, 2015 at 2:08
  • @sam I know that it's going to return "a value" but I would like to concatenate the answer in the for loop with the the answer outside of the loop, and then return that. So is there a way to store what's inside of the loop without creating a print statement? Commented Oct 31, 2015 at 2:11
  • 1
    Yes. You have multiple answers now :) Commented Oct 31, 2015 at 2:15

4 Answers 4

2

I think you're looking to build a recursive method here (which you might with a one-line ternary operation) like

return (size == 1) ? "?" : "?,"+func(size-1);

or you could do it iteratively with a StringBuilder like

StringBuilder sb = new StringBuilder();
for (int i = 0; i < size; i++)
{
    if (i != 0) {
        sb.append(", ");
    }
    sb.append("?");
}
return sb.toString();
Sign up to request clarification or add additional context in comments.

Comments

0

Editted the code a little bit, you were trying to use return statements within a for loop. You have to understand that once a return statement is called, the specified value is automatically taken out of the function. You cannot use multiple versions of this. Try the code below and see if that works. Ive made a string, and added "?," to it each time it needs to.

public static void main(String[] args)
{
  Scanner kb = new Scanner(System.in);
        r r = new r();
        System.out.println(r.func(3));
}

  public static String func(int size)
  {
     String value = "";
     if(size == 1)
        return "?";
     else {
        for (int i = 1; i < size; i++){
        value += "?,";
        }

        value+= "?";
        return value;
}

}

1 Comment

Thats alright! Don't forget to up-vote so others with the same problem can be helped out :-)
0

try not to return in for loop..

public static String func(int size)
      {String retval ="";
        if(size == 1)
        return "?";
        else
        {
        for (int i = 1; i < size; i++)
        {
            retval= retval+"?,";
          }
     return retval+"?";
        }
      }

Comments

0

The return keyword immediately stops executing a code inside a method and passes the value to the place of call.

You have to remove the return from the loop and place it after. Next you must save somewhere a current result - your question marks separated by a commas.


Let's define the new variable named result of type String and initialize it with empty string:

String result = "";

Second we have to write our loop and iterate over values between 1 to X (in this example: your size variable):

for(int i = 1; i <= size; i++) {
    result += "?,";
}

After that we have proper number of question marks inside the result variable. It's time to use the return keyword to pass the value to place where you called the method:

return result;

Now the println method inside the main method will get what we returned - string with question marks.

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.