D
D
Dmitry072016-09-02 10:20:39
Java
Dmitry07, 2016-09-02 10:20:39

How to add two lines correctly?

Good day.
There is a simple task: to add two strings, eliminating the repeated characters 'at the junction' of these strings. So, from 'abc' and 'bcd' we get the string 'abcd'.
This resulted in the following cycle:

public String conCat(String a, String b) {
  int len = a.length();
  String c = "";
      
  for (int i = 0; i < b.length(); i++);
    if (a.charAt(len - 1 - i).equals(b.charAt(i)))
      c += "";
    else
      c += b.charAt(i);

  return a + c;
}

However, eclipse indicated: `i cannot be resolved to a variable`. With what it can be connected? Is it possible to solve the problem without a loop?
Thanks to.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
E
Evhen, 2016-09-02
@Dmitry07

for (int i = 0; i < b.length(); i++); no semicolon needed here

A
aol-nnov, 2016-09-02
@aol-nnov

fall in love with curly braces already and make it a rule to put them everywhere (even where you could omit them).
and there will be no such questions.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question