2
public  String starString(int n){               
    int m = (int)Math.pow(2,n);
    String str="";
    str = starString(m-1,str);                  
    return str;
}
private  String starString(int n, String str){          
    String temp ="";
    if (n<0) {
        try{
            throw new IllegalArgumentException();
        } 
        catch(IllegalArgumentException ex){               
        }          
    }
    else { 
        temp+=("*");              
        starString(n-1,str);        
    }                       
    return temp;       
}

Can someone please explain to me why this code returns a single asterisk even if its called by a value greater than n >= 0?

I debugged and noticed that after throwing the exception it recurses again and all the asterisks get chopped to "". I've tried it many times. Its also required that you should throw the IllegalArgumentException if n < 0.

3
  • Why was this question downvoted? It could be better, yes, but it's a legitimate question from someone still learning to code in Java. Commented May 10, 2012 at 19:21
  • Don't use exceptions for expected conditions; exceptions are meant to be used for exceptional occurences, not as a signal that recursion is finished. Commented May 10, 2012 at 19:21
  • What is that exception even meant for? You throw it in a try-block and thus it is caught immediately. Commented May 10, 2012 at 19:24

2 Answers 2

6

In Java strings are immuntable, hence you need to assign a new value to temp (and pass temp as the parameter):

temp = starString(n-1, temp);        

Additionally you'd need to assign str to temp, otherwise each recursion would just return a single asterisk:

String temp = str;

A much simpler, cleaner (and correct) version of your recursive method would look like this:

private  String starString(int n){          
  String temp = "*";

  //only recurse as long as n > 0, i.e. the last invocation would be made with n = 0
  if (n > 0){ 
    temp += starString(n-1);                     
  }                      
  return temp;       
}

Note that you don't even need to pass the string as a parameter. Also note that recursion is overkill here, using a loop would make much nore sense. Also note that string concatenation is costly and gets slow quickly for higher values of n (due to immutable string instances being created over and over again). In that case you'd better use StringBuilder:

private  String starString(int n){          
  StringBuilder s = new StringBuilder();
  for( int i = 0; i <= n; i++ ) {
    s.append("*");
  }
  return s.toString();
}

On my machine a loop version using string concatenation takes around 12 seconds for n = 100000 whereas the StringBuilder version takes 0.007 seconds.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks...but i got it through the recursion quarter. so loop iterations are out of the question.
4

Your code invokes every recursion, stores a local temp, returns this and it is never used.

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.