I am working on this editDistance program in Java, but I cant figure out how to return the new strings.....
The program is returning the editDistance, but everytime a char doesnt match I want to delete it out of the string, in the end when I call printDistance(), I want to print the new Strings as well

import java.io.*;
import java.util.Scanner;
/**
 *
 * @author tricket_7
 */
public class NormalizedEditDistance {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) throws IOException {
        // TODO code application logic here
        //Get the files
       Scanner keyboard = new Scanner(System.in);
        System.out.println("Enter the first file: ");
        String filename1 = keyboard.nextLine();
        System.out.println("Enter the second file: ");
        String filename2 = keyboard.nextLine();

        //Open file 1
        File file1 = new File(filename1);
        Scanner inputFile1 = new Scanner(file1);

        //Read file 1
        String str1 = inputFile1.nextLine();

        //Open file2       
        File file2 = new File(filename2);
        Scanner inputFile2 = new Scanner(file2);
        String str2 = inputFile2.nextLine();


        //Close the file
        inputFile1.close();
        inputFile2.close();       

    for (int i = 1; i < args.length; i += 2)
      printDistance(args[i - 1], args[i]);    

      printDistance(str1, str2);


  }    

 public static int getDistance(String s, String t){

    int m = s.length();
    int n = t.length();
    int[][]dist = new int[m+1][n+1];
    for(int i = 0;i <= m;i++){
      dist[i][0] = i;

    }
    for(int j = 0;j <= n;j++){
      dist[0][j] = j;

    }
    for(int j = 1;j <= n;j++){
      for(int i = 1;i <= m;i++){
        if(s.charAt(i-1) != t.charAt(j-1)){        
         dist[i][j] = minDistance((dist[i-1][j]+1),(dist[i][j-1]+1),(dist[i-1][j-1]+1));          
        }
        else{         
         dist[i][j] = dist[i-1][j-1];
        }
      }
    }
    return(dist[m][n]);

  }


  public static int minDistance(int a,int b,int c){
    return(Math.min(Math.min(a,b),c));
  }
  public static void printDistance(String s, String t) {
    System.out.println(getDistance(s, t));
  }

}

Recommended Answers

All 6 Replies

what exactly do you mean 'when a char doesn't match'?

I read two files each containing one string, they are str1 and str2
originally I thought I would do: str1.toCharArray(), but I quicly figured out that I cannot delete an element from the array.

The edit distance program is computing the edit distance, but how do i return the two strings after the elements have been deleted?

Use the *.toString() method, where * is the name of the variable that you want changed to a String.

can not delete an element from the array???
and .... why not?

I am not sure how to delete an element from an array

Arrays are fixed size, so all you can do is either
replace the unwanted entry by some "empty" value (eg a null)
or
Create a new array one size smaller and copy all the elements except the deleted one into the new array.

This is why people tend to use Lists (eg LinkedList) instead of arrays when they need to do a lot of insertions or deletions.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.