Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7922191
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T16:52:14+00:00 2026-06-03T16:52:14+00:00

public String starString(int n){ int m = (int)Math.pow(2,n); String str=; str = starString(m-1,str); return

  • 0
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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-03T16:52:16+00:00Added an answer on June 3, 2026 at 4:52 pm

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Here is a VB.NET code snippet Public Class OOPDemo Private _strtString as String Public
public string[] SearchForMovie(string SearchParameter) { WebClientX.DownloadDataCompleted += new DownloadDataCompletedEventHandler(WebClientX_DownloadDataCompleted); WebClientX.DownloadDataAsync(new Uri( http://www.imdb.com/find?s=all&q=ironman+&x=0&y=0 )); string
public class Account { public string Username { get { return Username; } set
I have this: public string Log { get { return log; } protected set
I have a following class defined @JsonTypeName(PhotoSetUpdater) public class PhotoSetUpdater { @JsonProperty(Title) private String
public class Address { public string ZipCode {get; set;} } public class Customer {
public class Song { public string Genre { get; protected set; } public string
public class Mailing { public string To { get; set; } public string AttachmentPath
Public class AbcViewModel { public string native{get; set;} public string other{get; set;} public List<AbcViewModel>
public class ReflectionBase { public String ParentProperty1 { get; set; } public String ParentProperty2

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.