Receiving an OutOfBounds for a method that needs to build a string through recursion, not sure whyScanner cannot be resolved to a typeHow can I “delimit” an integer from a given string?Why reading byte array to an Object throws java.io.StreamCorruptedException?Java - Method executed prior to Default ConstructorKeep getting this error message for my RockPaperScissor Program. How do I fix this error?I/P-a string S.O/P: For each digit start from 0-9,print count of their occurrence in S.Print10 lines,each line contain 2 space separated integersstringbuilder in for loop with if statementWould it make any difference giving arguments using scanner class instead of command line arguments?Why does my method say it must return a type string and gives me an error even though my method does return a type string
Connect neutrals together in 3-gang box (load side) with 3x 3-way switches?
Construct a pentagon avoiding compass use
Variation in the spelling of word-final M
Why do legislative committees exist?
Possible isometry groups of open manifolds
Remove intersect line for one circle using venndiagram2sets
Why does the trade federation become so alarmed upon learning the ambassadors are Jedi Knights?
What is the closed form of the following recursive function?
I quit, and boss offered me 3 month "grace period" where I could still come back
I do not have power to all my breakers
3D-Plot with an inequality condition for parameter values
Nested-Loop-Join: How many comparisons and how many pages-accesses?
Does optical correction give a more aesthetic look to the SBI logo?
Cherries + 2% salt + 1 week at room temperature =?
Why do mean value theorems have open interval for differentiablity while closed for continuity?
Are villager price increases due to killing them temporary?
Won 50K! Now what should I do with it
How to make "plastic" sounding distored guitar
How are "soeben" and "eben" different from one another?
Why didn't Al Powell investigate the lights at the top of the building?
Can I activate an iPhone without an Apple ID?
Too many spies!
Why doesn't Anakin's lightsaber explode when it's chopped in half on Geonosis?
Is it okay to retroactively change things when running a published adventure?
Receiving an OutOfBounds for a method that needs to build a string through recursion, not sure why
Scanner cannot be resolved to a typeHow can I “delimit” an integer from a given string?Why reading byte array to an Object throws java.io.StreamCorruptedException?Java - Method executed prior to Default ConstructorKeep getting this error message for my RockPaperScissor Program. How do I fix this error?I/P-a string S.O/P: For each digit start from 0-9,print count of their occurrence in S.Print10 lines,each line contain 2 space separated integersstringbuilder in for loop with if statementWould it make any difference giving arguments using scanner class instead of command line arguments?Why does my method say it must return a type string and gives me an error even though my method does return a type string
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;
I am creating a method my compsci class that need to fulfill this task:
"Write a recursive method called String buildWeirdString(String s) While building the ‘forward’ part of the original string, it should not keep any digits that were part of the initial string - and while building the ‘backward’ part of the original string, it should not keep any letters of the alphabet that were part of the initial input. (you should find the reverseLine() method we did in class helpful) Tiny hint for ignoring case – there is a helpful method in the Character class that will tell you if a character is a letter or not. for example,
userinput:abCD123e!$f%
would produce the output:abCDe!$f%$!321
I don't exactly know how to use the Stringbuilder class because we haven't gone over it in class yet, but it seems like it would be useful for this application, so it is very likely my error is it its usage but it very well could be anywhere else.
This task begins at Static String buildWeirdString, the rest is other already completed tasks.
package recursionPrograms;
import java.util.*;
public class recursion
public static void main(String[] args)
Scanner s = new Scanner(System.in);
Scanner st = new Scanner(System.in);
int b = 0;
int e = 0;
String str;
String string;
// exp
System.out.println("Base: ");
b = s.nextInt();
System.out.println("Exponent: ");
e = s.nextInt();
System.out.println(recPow(b, e));
// palidrome
System.out.println("String: ");
str = st.nextLine();
System.out.println(isPalindrome(str));
//string
System.out.println("String: ");
string = st.nextLine();
System.out.println(buildWeirdString(string));
public static double recPow(int base, int exp)
if (exp == 0)
return 1;
else if (exp < 0)
return 1 / (base * recPow(base, Math.abs(exp) - 1));
else
return base * recPow(base, exp - 1);
static boolean isPalindrome(String str)
if (str.length() < 2)
return true;
if (str.charAt(0) == str.charAt(str.length() - 1))
return isPalindrome(str.substring(1, (str.length() - 1)));
return false;
static String buildWeirdString(String string)
StringBuilder stb = new StringBuilder();
StringBuilder stb2 = new StringBuilder();
if (string.length() == 0)
String rtrn = stb.toString() + stb2.reverse().toString();
return rtrn;
if (Character.isLetter(string.charAt(0)))
buildWeirdString(string.substring(1));
stb.insert(string.length() - 1,
Character.toString(string.charAt(0)));
if (!Character.isLetter(string.charAt(0)))
buildWeirdString(string.substring(1));
stb2.insert(string.length() - 1,
Character.toString(string.charAt(0)));
return null;
Desired results:
in: abc1d234
out: abcd4321
What happens:
Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: 1
at java.lang.AbstractStringBuilder.insert(Unknown Source)
at java.lang.StringBuilder.insert(Unknown Source)
at recursionPrograms.recursion.buildWeirdString(recursion.java:62)
at recursionPrograms.recursion.buildWeirdString(recursion.java:61)
at recursionPrograms.recursion.buildWeirdString(recursion.java:61)
at recursionPrograms.recursion.buildWeirdString(recursion.java:57)
at recursionPrograms.recursion.buildWeirdString(recursion.java:57)
at recursionPrograms.recursion.buildWeirdString(recursion.java:57)
at recursionPrograms.recursion.buildWeirdString(recursion.java:57)
at recursionPrograms.recursion.buildWeirdString(recursion.java:57)
at recursionPrograms.recursion.main(recursion.java:25)
java recursion stringbuilder
add a comment |
I am creating a method my compsci class that need to fulfill this task:
"Write a recursive method called String buildWeirdString(String s) While building the ‘forward’ part of the original string, it should not keep any digits that were part of the initial string - and while building the ‘backward’ part of the original string, it should not keep any letters of the alphabet that were part of the initial input. (you should find the reverseLine() method we did in class helpful) Tiny hint for ignoring case – there is a helpful method in the Character class that will tell you if a character is a letter or not. for example,
userinput:abCD123e!$f%
would produce the output:abCDe!$f%$!321
I don't exactly know how to use the Stringbuilder class because we haven't gone over it in class yet, but it seems like it would be useful for this application, so it is very likely my error is it its usage but it very well could be anywhere else.
This task begins at Static String buildWeirdString, the rest is other already completed tasks.
package recursionPrograms;
import java.util.*;
public class recursion
public static void main(String[] args)
Scanner s = new Scanner(System.in);
Scanner st = new Scanner(System.in);
int b = 0;
int e = 0;
String str;
String string;
// exp
System.out.println("Base: ");
b = s.nextInt();
System.out.println("Exponent: ");
e = s.nextInt();
System.out.println(recPow(b, e));
// palidrome
System.out.println("String: ");
str = st.nextLine();
System.out.println(isPalindrome(str));
//string
System.out.println("String: ");
string = st.nextLine();
System.out.println(buildWeirdString(string));
public static double recPow(int base, int exp)
if (exp == 0)
return 1;
else if (exp < 0)
return 1 / (base * recPow(base, Math.abs(exp) - 1));
else
return base * recPow(base, exp - 1);
static boolean isPalindrome(String str)
if (str.length() < 2)
return true;
if (str.charAt(0) == str.charAt(str.length() - 1))
return isPalindrome(str.substring(1, (str.length() - 1)));
return false;
static String buildWeirdString(String string)
StringBuilder stb = new StringBuilder();
StringBuilder stb2 = new StringBuilder();
if (string.length() == 0)
String rtrn = stb.toString() + stb2.reverse().toString();
return rtrn;
if (Character.isLetter(string.charAt(0)))
buildWeirdString(string.substring(1));
stb.insert(string.length() - 1,
Character.toString(string.charAt(0)));
if (!Character.isLetter(string.charAt(0)))
buildWeirdString(string.substring(1));
stb2.insert(string.length() - 1,
Character.toString(string.charAt(0)));
return null;
Desired results:
in: abc1d234
out: abcd4321
What happens:
Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: 1
at java.lang.AbstractStringBuilder.insert(Unknown Source)
at java.lang.StringBuilder.insert(Unknown Source)
at recursionPrograms.recursion.buildWeirdString(recursion.java:62)
at recursionPrograms.recursion.buildWeirdString(recursion.java:61)
at recursionPrograms.recursion.buildWeirdString(recursion.java:61)
at recursionPrograms.recursion.buildWeirdString(recursion.java:57)
at recursionPrograms.recursion.buildWeirdString(recursion.java:57)
at recursionPrograms.recursion.buildWeirdString(recursion.java:57)
at recursionPrograms.recursion.buildWeirdString(recursion.java:57)
at recursionPrograms.recursion.buildWeirdString(recursion.java:57)
at recursionPrograms.recursion.main(recursion.java:25)
java recursion stringbuilder
Your methodbuildWeirdString
truly does what its name promises. First, theif (string.length() == 0) String rtrn = stb.toString() + stb2.reverse().toString(); return rtrn;
; since bothStringBuilder
instances are empty at this point, neither reversing one nor concatenating both makes any sense, the result will be just an empty string so why not return either, the argumentstring
which you just proved to be an empty string, or just""
? Then, the only otherreturn
statement is thereturn null;
, so regardless of what’s happening in the middle, there are only two possible results.
– Holger
Mar 27 at 16:57
But well, you’re never using the results of the recursive call anyway. The method creates two, initially emptyStringBuilder
instances, and if the argument is not an empty string, it does a recursive invocation, ignoring the result and trying to insert atstring.length() - 1
into one of theStringBuilder
instances. Since they are empty, any nonzero index will cause an exception.
– Holger
Mar 27 at 17:38
Gotcha, thank you for the help!
– chris.macri
Apr 3 at 15:22
add a comment |
I am creating a method my compsci class that need to fulfill this task:
"Write a recursive method called String buildWeirdString(String s) While building the ‘forward’ part of the original string, it should not keep any digits that were part of the initial string - and while building the ‘backward’ part of the original string, it should not keep any letters of the alphabet that were part of the initial input. (you should find the reverseLine() method we did in class helpful) Tiny hint for ignoring case – there is a helpful method in the Character class that will tell you if a character is a letter or not. for example,
userinput:abCD123e!$f%
would produce the output:abCDe!$f%$!321
I don't exactly know how to use the Stringbuilder class because we haven't gone over it in class yet, but it seems like it would be useful for this application, so it is very likely my error is it its usage but it very well could be anywhere else.
This task begins at Static String buildWeirdString, the rest is other already completed tasks.
package recursionPrograms;
import java.util.*;
public class recursion
public static void main(String[] args)
Scanner s = new Scanner(System.in);
Scanner st = new Scanner(System.in);
int b = 0;
int e = 0;
String str;
String string;
// exp
System.out.println("Base: ");
b = s.nextInt();
System.out.println("Exponent: ");
e = s.nextInt();
System.out.println(recPow(b, e));
// palidrome
System.out.println("String: ");
str = st.nextLine();
System.out.println(isPalindrome(str));
//string
System.out.println("String: ");
string = st.nextLine();
System.out.println(buildWeirdString(string));
public static double recPow(int base, int exp)
if (exp == 0)
return 1;
else if (exp < 0)
return 1 / (base * recPow(base, Math.abs(exp) - 1));
else
return base * recPow(base, exp - 1);
static boolean isPalindrome(String str)
if (str.length() < 2)
return true;
if (str.charAt(0) == str.charAt(str.length() - 1))
return isPalindrome(str.substring(1, (str.length() - 1)));
return false;
static String buildWeirdString(String string)
StringBuilder stb = new StringBuilder();
StringBuilder stb2 = new StringBuilder();
if (string.length() == 0)
String rtrn = stb.toString() + stb2.reverse().toString();
return rtrn;
if (Character.isLetter(string.charAt(0)))
buildWeirdString(string.substring(1));
stb.insert(string.length() - 1,
Character.toString(string.charAt(0)));
if (!Character.isLetter(string.charAt(0)))
buildWeirdString(string.substring(1));
stb2.insert(string.length() - 1,
Character.toString(string.charAt(0)));
return null;
Desired results:
in: abc1d234
out: abcd4321
What happens:
Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: 1
at java.lang.AbstractStringBuilder.insert(Unknown Source)
at java.lang.StringBuilder.insert(Unknown Source)
at recursionPrograms.recursion.buildWeirdString(recursion.java:62)
at recursionPrograms.recursion.buildWeirdString(recursion.java:61)
at recursionPrograms.recursion.buildWeirdString(recursion.java:61)
at recursionPrograms.recursion.buildWeirdString(recursion.java:57)
at recursionPrograms.recursion.buildWeirdString(recursion.java:57)
at recursionPrograms.recursion.buildWeirdString(recursion.java:57)
at recursionPrograms.recursion.buildWeirdString(recursion.java:57)
at recursionPrograms.recursion.buildWeirdString(recursion.java:57)
at recursionPrograms.recursion.main(recursion.java:25)
java recursion stringbuilder
I am creating a method my compsci class that need to fulfill this task:
"Write a recursive method called String buildWeirdString(String s) While building the ‘forward’ part of the original string, it should not keep any digits that were part of the initial string - and while building the ‘backward’ part of the original string, it should not keep any letters of the alphabet that were part of the initial input. (you should find the reverseLine() method we did in class helpful) Tiny hint for ignoring case – there is a helpful method in the Character class that will tell you if a character is a letter or not. for example,
userinput:abCD123e!$f%
would produce the output:abCDe!$f%$!321
I don't exactly know how to use the Stringbuilder class because we haven't gone over it in class yet, but it seems like it would be useful for this application, so it is very likely my error is it its usage but it very well could be anywhere else.
This task begins at Static String buildWeirdString, the rest is other already completed tasks.
package recursionPrograms;
import java.util.*;
public class recursion
public static void main(String[] args)
Scanner s = new Scanner(System.in);
Scanner st = new Scanner(System.in);
int b = 0;
int e = 0;
String str;
String string;
// exp
System.out.println("Base: ");
b = s.nextInt();
System.out.println("Exponent: ");
e = s.nextInt();
System.out.println(recPow(b, e));
// palidrome
System.out.println("String: ");
str = st.nextLine();
System.out.println(isPalindrome(str));
//string
System.out.println("String: ");
string = st.nextLine();
System.out.println(buildWeirdString(string));
public static double recPow(int base, int exp)
if (exp == 0)
return 1;
else if (exp < 0)
return 1 / (base * recPow(base, Math.abs(exp) - 1));
else
return base * recPow(base, exp - 1);
static boolean isPalindrome(String str)
if (str.length() < 2)
return true;
if (str.charAt(0) == str.charAt(str.length() - 1))
return isPalindrome(str.substring(1, (str.length() - 1)));
return false;
static String buildWeirdString(String string)
StringBuilder stb = new StringBuilder();
StringBuilder stb2 = new StringBuilder();
if (string.length() == 0)
String rtrn = stb.toString() + stb2.reverse().toString();
return rtrn;
if (Character.isLetter(string.charAt(0)))
buildWeirdString(string.substring(1));
stb.insert(string.length() - 1,
Character.toString(string.charAt(0)));
if (!Character.isLetter(string.charAt(0)))
buildWeirdString(string.substring(1));
stb2.insert(string.length() - 1,
Character.toString(string.charAt(0)));
return null;
Desired results:
in: abc1d234
out: abcd4321
What happens:
Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: 1
at java.lang.AbstractStringBuilder.insert(Unknown Source)
at java.lang.StringBuilder.insert(Unknown Source)
at recursionPrograms.recursion.buildWeirdString(recursion.java:62)
at recursionPrograms.recursion.buildWeirdString(recursion.java:61)
at recursionPrograms.recursion.buildWeirdString(recursion.java:61)
at recursionPrograms.recursion.buildWeirdString(recursion.java:57)
at recursionPrograms.recursion.buildWeirdString(recursion.java:57)
at recursionPrograms.recursion.buildWeirdString(recursion.java:57)
at recursionPrograms.recursion.buildWeirdString(recursion.java:57)
at recursionPrograms.recursion.buildWeirdString(recursion.java:57)
at recursionPrograms.recursion.main(recursion.java:25)
java recursion stringbuilder
java recursion stringbuilder
edited Mar 26 at 6:27
Patrick Parker
3,5582 gold badges10 silver badges35 bronze badges
3,5582 gold badges10 silver badges35 bronze badges
asked Mar 26 at 1:37
chris.macrichris.macri
61 bronze badge
61 bronze badge
Your methodbuildWeirdString
truly does what its name promises. First, theif (string.length() == 0) String rtrn = stb.toString() + stb2.reverse().toString(); return rtrn;
; since bothStringBuilder
instances are empty at this point, neither reversing one nor concatenating both makes any sense, the result will be just an empty string so why not return either, the argumentstring
which you just proved to be an empty string, or just""
? Then, the only otherreturn
statement is thereturn null;
, so regardless of what’s happening in the middle, there are only two possible results.
– Holger
Mar 27 at 16:57
But well, you’re never using the results of the recursive call anyway. The method creates two, initially emptyStringBuilder
instances, and if the argument is not an empty string, it does a recursive invocation, ignoring the result and trying to insert atstring.length() - 1
into one of theStringBuilder
instances. Since they are empty, any nonzero index will cause an exception.
– Holger
Mar 27 at 17:38
Gotcha, thank you for the help!
– chris.macri
Apr 3 at 15:22
add a comment |
Your methodbuildWeirdString
truly does what its name promises. First, theif (string.length() == 0) String rtrn = stb.toString() + stb2.reverse().toString(); return rtrn;
; since bothStringBuilder
instances are empty at this point, neither reversing one nor concatenating both makes any sense, the result will be just an empty string so why not return either, the argumentstring
which you just proved to be an empty string, or just""
? Then, the only otherreturn
statement is thereturn null;
, so regardless of what’s happening in the middle, there are only two possible results.
– Holger
Mar 27 at 16:57
But well, you’re never using the results of the recursive call anyway. The method creates two, initially emptyStringBuilder
instances, and if the argument is not an empty string, it does a recursive invocation, ignoring the result and trying to insert atstring.length() - 1
into one of theStringBuilder
instances. Since they are empty, any nonzero index will cause an exception.
– Holger
Mar 27 at 17:38
Gotcha, thank you for the help!
– chris.macri
Apr 3 at 15:22
Your method
buildWeirdString
truly does what its name promises. First, the if (string.length() == 0) String rtrn = stb.toString() + stb2.reverse().toString(); return rtrn;
; since both StringBuilder
instances are empty at this point, neither reversing one nor concatenating both makes any sense, the result will be just an empty string so why not return either, the argument string
which you just proved to be an empty string, or just ""
? Then, the only other return
statement is the return null;
, so regardless of what’s happening in the middle, there are only two possible results.– Holger
Mar 27 at 16:57
Your method
buildWeirdString
truly does what its name promises. First, the if (string.length() == 0) String rtrn = stb.toString() + stb2.reverse().toString(); return rtrn;
; since both StringBuilder
instances are empty at this point, neither reversing one nor concatenating both makes any sense, the result will be just an empty string so why not return either, the argument string
which you just proved to be an empty string, or just ""
? Then, the only other return
statement is the return null;
, so regardless of what’s happening in the middle, there are only two possible results.– Holger
Mar 27 at 16:57
But well, you’re never using the results of the recursive call anyway. The method creates two, initially empty
StringBuilder
instances, and if the argument is not an empty string, it does a recursive invocation, ignoring the result and trying to insert at string.length() - 1
into one of the StringBuilder
instances. Since they are empty, any nonzero index will cause an exception.– Holger
Mar 27 at 17:38
But well, you’re never using the results of the recursive call anyway. The method creates two, initially empty
StringBuilder
instances, and if the argument is not an empty string, it does a recursive invocation, ignoring the result and trying to insert at string.length() - 1
into one of the StringBuilder
instances. Since they are empty, any nonzero index will cause an exception.– Holger
Mar 27 at 17:38
Gotcha, thank you for the help!
– chris.macri
Apr 3 at 15:22
Gotcha, thank you for the help!
– chris.macri
Apr 3 at 15:22
add a comment |
0
active
oldest
votes
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55348669%2freceiving-an-outofbounds-for-a-method-that-needs-to-build-a-string-through-recur%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
0
active
oldest
votes
0
active
oldest
votes
active
oldest
votes
active
oldest
votes
Is this question similar to what you get asked at work? Learn more about asking and sharing private information with your coworkers using Stack Overflow for Teams.
Is this question similar to what you get asked at work? Learn more about asking and sharing private information with your coworkers using Stack Overflow for Teams.
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55348669%2freceiving-an-outofbounds-for-a-method-that-needs-to-build-a-string-through-recur%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Your method
buildWeirdString
truly does what its name promises. First, theif (string.length() == 0) String rtrn = stb.toString() + stb2.reverse().toString(); return rtrn;
; since bothStringBuilder
instances are empty at this point, neither reversing one nor concatenating both makes any sense, the result will be just an empty string so why not return either, the argumentstring
which you just proved to be an empty string, or just""
? Then, the only otherreturn
statement is thereturn null;
, so regardless of what’s happening in the middle, there are only two possible results.– Holger
Mar 27 at 16:57
But well, you’re never using the results of the recursive call anyway. The method creates two, initially empty
StringBuilder
instances, and if the argument is not an empty string, it does a recursive invocation, ignoring the result and trying to insert atstring.length() - 1
into one of theStringBuilder
instances. Since they are empty, any nonzero index will cause an exception.– Holger
Mar 27 at 17:38
Gotcha, thank you for the help!
– chris.macri
Apr 3 at 15:22