Results 1 to 1 of 1
  1. #1
    fam2315 is offline Member
    Join Date
    Feb 2011
    Rep Power

    Default Recursively searching a string

    I have to create a class called Statement, that simply has a method in it called find(), which takes in a String as an argument, and returns a boolean, representing whether or not the Statement contains that String.

    I think I have a solution, but I'm trying to do it without utilizing a temp variable inside my class, can anyone suggest a way? The problem is that we were told to approach it by analyzing the substring over and over (recursively), and I cant think of how to do that without creating a temporary member variable. Here is what I have:

    Java Code:
    public class Statement{
        private String statementPassedIn;
        private String tempStatement;
        public Statement(String statementPassedIn) {
            this.statementPassedIn= statementPassedIn;
            tempStatement= statementPassedIn;
        public boolean find(String searchString) {
            if (tempStatement.indexOf(searchString) == 0) {
                tempStatement= statementPassedIn;
                return true;
            tempStatement= tempSentence.substring(1);
            if (tempStatement!= null && tempStatement.length() >= searchString.length()) {
                return find(searchString);
            tempStatement= statementPassedIn;
            return false;
    It seems to work, but the idea of resetting the tempStatement variable right before the return statement makes me think I shouldn't need to have to declare a member variable (tempStatement) in order to achieve this. The constructor just takes in the actual statement, and sets the tempStatement and statementPassedIn values. The example I used is:

    Java Code:
    Statement myStatement = "abc";
    Any input on the use of a temp variable, just doesn't seem right to me.

    I know I need to add null trapping, but right now, I'm just concerned with the temp variable, whether or not its necessary. I do have to do this recursively though. The other thing that's weird is that the find() outline that was given takes in the searchString, but that will never change, so using recursion and passing the same thing in each time seemed weird too.
    Last edited by fam2315; 10-13-2012 at 06:27 PM.

Similar Threads

  1. Searching for a substring within a String
    By Tryx in forum New To Java
    Replies: 7
    Last Post: 07-16-2012, 08:12 PM
  2. searching a string in a file
    By sathya11491 in forum New To Java
    Replies: 4
    Last Post: 03-27-2012, 01:12 PM
  3. Searching for a string in different log files
    By nitin kishore in forum Advanced Java
    Replies: 6
    Last Post: 09-14-2011, 09:32 PM
  4. Replies: 8
    Last Post: 11-20-2009, 09:33 PM
  5. recursively searching through arraylists
    By newtojava7 in forum New To Java
    Replies: 1
    Last Post: 03-17-2008, 02:36 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts