-1

I am beginner (newbie) in Java. I have downloaded the Eclipse environment to be able to code and run Java programs. Now my problem is simple: I am trying to run the code below, specifically I want to compute the so called edit distance between two words. I do not know how insert an input to this code (very simple right!). Take e.g. w1=labd, w2=blad and w1len=4, w2len=4. How do I insert this as an argument to my function?

CODE

int partDist(String w1, String w2, int w1len, int w2len) {
    if (w1len == 0)
      return w2len;
    if (w2len == 0)
      return w1len;
    int res = partDist(w1, w2, w1len - 1, w2len - 1) + 
    (w1.charAt(w1len - 1) == w2.charAt(w2len - 1) ? 0 : 1);
    int addLetter = partDist(w1, w2, w1len - 1, w2len) + 1;
    if (addLetter < res)
      res = addLetter;
    int deleteLetter = partDist(w1, w2, w1len, w2len - 1) + 1;
    if (deleteLetter < res)
      res = deleteLetter;
    return res;
  }

Should I add some command at the end of the code and run it? What would that be for this particular case?

1 Answers1

1

Place your function 'partDist' inside a class along with a main function and call it from main function like this :

    class YourMainClass{
    public static void main(String args[]){
       System.out.println(partDist("labd", "blad", 4, 4)); // inorder to print the result to console
    }
    int partDist(String w1, String w2, int w1len, int w2len) {
    if (w1len == 0)
      return w2len;
    if (w2len == 0)
      return w1len;
    int res = partDist(w1, w2, w1len - 1, w2len - 1) + 
    (w1.charAt(w1len - 1) == w2.charAt(w2len - 1) ? 0 : 1);
    int addLetter = partDist(w1, w2, w1len - 1, w2len) + 1;
    if (addLetter < res)
      res = addLetter;
    int deleteLetter = partDist(w1, w2, w1len, w2len - 1) + 1;
    if (deleteLetter < res)
      res = deleteLetter;
    return res;
  }
  }
f1sh
  • 11,489
  • 3
  • 25
  • 51