This is a question from a past exam paper. I am not too sure how to convert the extract method to a while and for loop.
I have attempted this question: the extract1 and extract2 methods but i know they are incorrect. The original method may not be useful but the exam requires you to show how to write methods differently. I wanted to know how they could be done for future reference.
String extractedThis = "";
public String extract(String text){
if(text.length()==0){
return extractedThis;
} else {
return extractedThis = text.charAt(0) + extract(text.substring(1));
}
}
public String extract1(String text) {
while (text != null) {
extractedThis = text.charAt(0) + text.substring(1);
}
return extractedThis;
}
public String extract2(String text) {
for (int i = 0; i < text.length(); i++) {
extractedThis = text.substring(i);
}
return extractedThis;
}
However, I don’t see what exactly your trying to achieve with these methods as the return their input, and could be done much easier