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);
}