D
D
Dmitry072016-10-06 17:27:08
Java
Dmitry07, 2016-10-06 17:27:08

Why doesn't the program work without the second 'return'?

Good day.
There is a task within the framework of which it is required to remove all substrings (remove) from one string (base), case insensitive. The problem was solved in the following way:

public String withoutString(String base, String remove) {
  String result = "";
  
  for (int i = 0; i < (base.length()-remove.length()+1); i++)
    if (!base.substring(i, i+remove.length()).toLowerCase().equals(remove.toLowerCase()))
      result += base.substring(i, i+1);
    else if (i+remove.length() < base.length())
      i += remove.length()-1;
    else
      return result;
  
  for (int j = base.length()-remove.length()+1; j < base.length(); j++)
    result += base.substring(j, j+1);
  
  return result;
}

Without construction
else
  return result;

the program passes all checks except for the "Hi HoHo"/"Ho" case (output: "Hi o").
The question is why the `return` operator (the second one, in fact) is needed here, and the same `break` operator will not work here?
Thanks to.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
I
Igor, 2016-10-06
@DMGarikk

I would advise you to redo the solution of the problem, and not look for an error in this code

A
aol-nnov, 2016-10-06
@aol-nnov

ручной счет рулит. выполни по шагам алгоритм, и твой вопрос отпадет.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question