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 4 of 4

Thread: Recursive Methods ; StringIndexOutOfBoundsException

  1. #1
    Member
    Join Date
    Jul 2011
    Posts
    38
    My Mood
    Confused
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Recursive Methods ; StringIndexOutOfBoundsException

    This is a code to count how many times a character appeared in a String...
    It has to be done recursively...
    import java.io.*;
    public class Methods{
    	public static void main(String args []) throws IOException{
    		InputStreamReader instream = new InputStreamReader(System.in);
    		BufferedReader br = new BufferedReader (instream);
    		System.out.println("Please insert a passage");
    		String passage = br.readLine();
    		System.out.println("Please insert character");
    		String crc = br.readLine();
    		char x = crc.charAt(0);
    		System.out.println("This passage has this character " + crccount(passage, x)); 
    	}
    	public static int crccount(String passage, char x){
    		int count = 0;
    		int z = passage.length();
    		if (z == 0){
    			return 0;
    		}
    		if (passage.charAt(z) == x){
    			count++;
    		}
    		return count + crccount(passage.substring(0, z-1), x);
    	}
    }

    RUNTIME:
    Please insert a passage
    medo
    Please insert character
    e
    Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: 4
    at java.lang.String.charAt(String.java:686)
    at Methods.crccount(Methods.java:459)
    at Methods.main(Methods.java:451)


    Any suggestions?


  2. #2
    Junior Member
    Join Date
    Jul 2011
    Posts
    25
    My Mood
    Fine
    Thanks
    1
    Thanked 4 Times in 4 Posts

    Default Re: Recursive Methods ; StringIndexOutOfBoundsException

    change only 1 line
     
    package test;
     
    import java.io.*;
    public class Methods{
    	public static void main(String args []) throws IOException{
    		InputStreamReader instream = new InputStreamReader(System.in);
    		BufferedReader br = new BufferedReader (instream);
    		System.out.println("Please insert a passage");
    		String passage = br.readLine();
    		System.out.println("Please insert character");
    		String crc = br.readLine();
    		char x = crc.charAt(0);
    		System.out.println("This passage has this character " + crccount(passage, x)); 
    	}
    	public static int crccount(String passage, char x){
    		int count = 0;
    		int z = passage.length();
    		if (z == 0){
    			return 0;
    		}
                    else if (passage.charAt(z-1) == x){//just change this line 
    			count++;
    		}
    		return count + crccount(passage.substring(0, z-1), x);
    	}
    }
    I hope it will work

  3. The Following User Says Thank You to serdar For This Useful Post:

    Medo Almasry (July 18th, 2011)

  4. #3
    Member
    Join Date
    Jul 2011
    Posts
    38
    My Mood
    Confused
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: Recursive Methods ; StringIndexOutOfBoundsException

    THANKS ALOT MAN...
    I HONESTLY APPRECIATE IT...

  5. #4
    Member
    Join Date
    Jul 2011
    Posts
    38
    My Mood
    Confused
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: Recursive Methods ; StringIndexOutOfBoundsException

    I didn't notice charAt method starts at 0... Silly mistakes :@
    Thanks alot though

Similar Threads

  1. [SOLVED] StringIndexOutOfBoundsException
    By Medo Almasry in forum What's Wrong With My Code?
    Replies: 3
    Last Post: July 14th, 2011, 08:54 AM
  2. Convert this to non-recursive
    By IAmHere in forum Algorithms & Recursion
    Replies: 1
    Last Post: June 10th, 2011, 11:28 AM
  3. [SOLVED] StackOverflowError with recursive method
    By samfin in forum What's Wrong With My Code?
    Replies: 4
    Last Post: December 2nd, 2010, 04:05 PM
  4. Problem with Recursive code
    By Shadow703793 in forum Algorithms & Recursion
    Replies: 4
    Last Post: February 22nd, 2010, 09:36 PM
  5. [SOLVED] Recursive Sentence Finder
    By raphytaffy in forum Algorithms & Recursion
    Replies: 4
    Last Post: February 21st, 2010, 02:46 PM