Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 5 of 5

Thread: Recursion /String problem

  1. #1
    Junior Member
    Join Date
    Jan 2013
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Recursion /String problem

    So I am new to java and been practicing recursion with recursion practices from codingbat


    The problem im working on is this
    CodingBat Java Recursion-1 changePi


    and here's my code for it

    public class changePi {
     
     
    public String changeToPi(String str){
    	String total = "";
    	if(str.length() == 0)
    	return "";
     
    	if (str.substring(0, 4) == "3.14")
    	{
    		total = "pi";
     
    		total = total + changeToPi(str.substring(4));
     
     
    	}
    	else if (str.substring(0, 4) != "3.14")
    	{
    		total = str.substring(0,1);
    		total = total + changeToPi(str.substring(1));
     
    	}
     
    	return total;
     
    }
     
    public static void main (String args[]){
    	changePi um = new changePi();
    	um.changeToPi("3.14ihiuhui");
     
     
    }
     
    }

    problem is that whenever I try to run it i get a String index out of range error

    Been looking over the problem over and over again and can't spot what I did wrong. Would appreciate some help.
    Last edited by helloworld922; January 9th, 2013 at 04:08 AM. Reason: please use [code] tags


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Recursion /String problem

    i get a String index out of range error
    Please copy the full text of the error message and post it here.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Jan 2013
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Recursion /String problem

    Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: 4
    at java.lang.String.substring(Unknown Source)
    at changePi.changeToPi(changePi.java:12)
    at changePi.changeToPi(changePi.java:23)
    at changePi.changeToPi(changePi.java:23)
    at changePi.changeToPi(changePi.java:23)
    at changePi.changeToPi(changePi.java:23)
    at changePi.changeToPi(changePi.java:23)
    at changePi.changeToPi(changePi.java:23)
    at changePi.changeToPi(changePi.java:23)
    at changePi.changeToPi(changePi.java:23)
    at changePi.main(changePi.java:33)

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Recursion /String problem

    Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: 4
    at java.lang.String.substring(Unknown Source)
    at changePi.changeToPi(changePi.java:12)
    At line 12 the index to the String is past the end of the String
    To see what is happening, add a println() statement at line 11 that prints the String and its length.
    The printout will show you what String the code is working with.

    The test of the String length should make sure the length is >= 4 vs == 0
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Junior Member
    Join Date
    Jan 2013
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Recursion /String problem

    ok got it, will check
    sweet, got it to work! thx

    public class changePi {



    public String changeToPi(String str){
    String total = "";

    if (str.length() < 4 && !str.equals("3.14"))
    {
    return str;
    }


    if (str.length() >= 4)
    {
    if (str.substring(0, 4).equals("3.14"))
    {
    total = "pi";
    total = total + changeToPi(str.substring(4));
    }

    else if (!str.substring(0, 4).equals("3.14"))
    {
    total = str.substring(0,1);
    total = total + changeToPi(str.substring(1));

    }

    }


    return total;

    }

    public static void main (String args[]){
    changePi um = new changePi();
    String newPi;
    newPi = um.changeToPi("3.14ih3.14.56ih3ih");
    System.out.println(newPi);


    }

    }

Similar Threads

  1. Replies: 5
    Last Post: April 22nd, 2013, 07:27 AM
  2. Print a string in reverse using recursion
    By ManInTheMiddle in forum Algorithms & Recursion
    Replies: 5
    Last Post: December 1st, 2012, 08:30 AM
  3. Need help with Java recursion problem!
    By eyesackery in forum Java Theory & Questions
    Replies: 2
    Last Post: June 5th, 2012, 08:12 AM
  4. recursion problem, please help
    By nil in forum What's Wrong With My Code?
    Replies: 2
    Last Post: October 18th, 2010, 12:59 PM
  5. New Recursion problem need lil help
    By Delstateprogramer in forum Algorithms & Recursion
    Replies: 21
    Last Post: July 8th, 2010, 06:35 PM