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

  • SEARCH
  • Home
  • 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 6707985
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T07:42:17+00:00 2026-05-26T07:42:17+00:00

I am writing a function in Java for an Android application that uses a

  • 0

I am writing a function in Java for an Android application that uses a StringBuilder to generate all permutations of a string.

Whenever the function is run, the program instantly terminates, and the DDMS (Dalvic Virtual Machine debugging tool) claims a stack overflow within my function.

private void reorder(String reorder_this, StringBuilder in_this){

    for(int i = 0; i < reorder_this.length(); i++)
    {
        if(i == reorder_this.length())
        {
            in_this.append(System.getProperty("line.separator"));
        }
        else
        {
            in_this.append(reorder_this.charAt(i));
            reorder(reorder_this.substring(0, i) + reorder_this.substring(i), in_this);
        }
    }
}

You can see that I have taken a recursive approach to this problem, which I believe will end up filling the string builder with all possible permutations of the inputted string each followed by the newline character.

Does anybody have an idea about what could be causing the stack overflow?

  • 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-05-26T07:42:18+00:00Added an answer on May 26, 2026 at 7:42 am

    In short, your function cannot terminate unless your string has a length of 0.

    Your method begins with setting i to 0 and testing whether i is less than length of your first argument. If it is (which will be the case for all but empty strings), you immediately recurse because you can not be strictly less than the length and equal to the length. In your recursive call, you pass in a string of exactly the same length (indeed, the same exact string, as Thilo points out). This indicates a second problem with the algorithm: recursive algorithms should operate on “smaller” arguments for each recursive call.

    It will not take long to get a StackOverflowException here. Each recursive call pushes a new stack frame.

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

Sidebar

Related Questions

I'm writing a Java program, and I want a function that, given a string,
I'm writing an application (on android) that uses both java code, and native code.
I'm writing an import function of XML files to my Java application. I am
I'm writing a java servlet that calls a function in a jar. I have
Problem Description I'm writing module for Android (JAVA) that use native code (C) .
I'm writing an Android application that allows a user to maintain a list of
I'm writing a program in java that essentially tests a bunch of things... For
Whats this syntax useful for : function(String... args) Is this same as writing function(String[]
I'm writing a function that fishes out the src from the first image tag
I am writing a function in Haskell that deals with numbers beyond the length

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.