title: Stop and return value of a recursive method in java
body: <p>I am trying to do a recursive method which returns a some random characters by some rules. The method works great, but the problem is that it doesn't return the value i need nor stop when i write <code>return myString</code> </p>
  
  <p>here is the method(please take a look at comments):</p>
  
  <pre><code> public String RowGenerator(String sir) {
  
      upper = "";
  
      Log.v(TAG, "RowGenerator Started");
      int randI = 1 + (int)(Math.random() * 4);
      int randJ = 1 + (int)(Math.random() * 2);
      int randK = 1 + (int)(Math.random() * 2);
      Log.v(TAG, "inside RowGenerator: randI " + randI + ", randJ " + randJ + ", randK "+ randK);
  
      String upper = sir.replaceAll("[^A-Z]+", "");
      sir = sir.replaceAll("[^a-z]+", "");
  
      Log.v(TAG, "String upper " + upper);
  
  //checking if there is a..z &amp; sir&gt;=5
      if ((sir.length()&gt;=5) &amp;&amp; upper.isEmpty()) {
          Log.v(TAG, "sir &lt;5 &amp; !b " + String.valueOf(sir));  // here it prints out exactly what i need, but when i setText id has another value.
          sir1.setText(String.valueOf(sir));  // this textview does not get the right value.
          return sir; // i want to end the function here and return the value , as i call it as follows: someString = String.valueOf(RowGenerator(sir))
      } else if (sir.isEmpty()) {
          sir = S[1];
          Log.v(TAG, "First sir value: " + sir);
          RowGenerator(sir);
      }  else if (sir.length()&lt;5 &amp;&amp; upper.isEmpty()) {
          sir = "";
          RowGenerator(sir);
      }
       // if there is A..Z
      else if (!upper.equals(null)) {
          if(upper.equals(S[0])) {
              sir = sir + S[1];
          } else if (upper.equals(I[0])) {
              sir = sir + I[randI];
          } else if (upper.equals(J[0])) {
              sir = sir + J[randJ];
          } else if (upper.equals(K[0])) {
              sir = sir + K[randK];
          }
          Log.v(TAG, "new value added to sir: " + sir);
          RowGenerator(sir);
  
      }
      Log.v(TAG, "returning value ");  // everything goes great till here, but then this log appears like 5-20 times randomly at each start, it should end in first if, but it got till here
      sir1.setText(String.valueOf(sir));
      return sir; 
  }
  </code></pre>
  
  <p>as far as i know return <code>myString();</code> should stop the method and return the value, but it doesnt. please help me find what is wrong with my method. </p>
  
  <p>any help would be very much appreciated.</p>