1

I am trying to make a recursive class that takes a string as a parameter and returns a line from a stringlist that starts with the parameter string. If it cant find a match, it should return "". I seem to be almost there but for some reason once the string is found and enters the if statement, it returns the string, but then jumps to the other statement in the code. In other words, its not returning it for some reason. any help would really help.

public String getLineStartingWith(String _string){

    System.out.println("GETLINESTRING: " + _string);

    //place parameter string into local string
    String string = _string;

    //return the line from the stringlist if it starts with the 
    //parameter string

    if(currentString.startsWith(_string)){
        System.out.println("CURRENT STRING: " + currentString);
        return currentString;
    } 

    restOfList.getLineStartingWith(_string);
    return "";

    //return restOfList.getLineStartingWith(_string);
}

EDIT

I made some changes to my code. But for some reason, after it returns the current string, it returns restOfList.getLineStartingWith(string) everytime. I think its not detecting if its empty or not

public String getLineStartingWith(String string){

    System.out.println("GETLINESTRING: " + string);

    //return the line from the stringlist if it starts with the 
    //parameter string

    if(currentString.startsWith(string)){
       System.out.println("CURRENT STRING: " + currentString);
       return currentString;
    } 

    if(restOfList.isEmpty){
        return "";
    }

    return restOfList.getLineStartingWith(string);
}

2 Answers 2

1

Why are you doing this?

//place parameter string into local string
String string = _string;

This code has exactly the same effect:

public String getLineStartingWith(String string){

    System.out.println("GETLINESTRING: " + string);
    // etc.

String are immutable, so you can't do anything bad to the input. Also, you're not even using string (all of your later code uses _string).

I think your problem is that this isn't being returned:

restOfList.getLineStartingWith(_string);

Try:

return restOfList.getLineStartingWith(_string);

You probably also need another base-case that handles restOfList being empty.

Sign up to request clarification or add additional context in comments.

6 Comments

So the first part i just left that there for no reason. my bad. And when you mean base-case, what does that mean?
@SergioRa - In recursion, a base case is a situation where you know the answer. For example, in your program, the if(currentString.startsWith(_string)){ section is a base case, because you stop recursively calling the function at that point. You have a base case for when you have the answer, you just need one for when there is no answer.
Ok i am beginning to understand, I think. So something like this: if(restOfList.isEmpty){ return ""; } where is empty is a boolean.
@SergioRa - Exactly. And make sure to put it before the recursive call (restOfList.getLineStartingWith(_string);) to prevent an infinite loop.
I modified my code and added it above. i'm still having some issues though. Its as if it doesn't want to return the string and I end up getting a null pointer exception
|
0

I'm sorry if this might not answer your question.

It seems to me that you just want to display all the lines that start with that particular String. How about a simple loop on each item in the String list? How about something like this:

for (String line : allLines) {
    if (line.startsWith(string)){
        System.out.println(line);
    }
}

Sorry if this is not what you want.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.