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

    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));          
         dist[i][j] = dist[i-1][j-1];


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

3 Years
Discussion Span
Last Post by JamesCherrill

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?


Arrays are fixed size, so all you can do is either
replace the unwanted entry by some "empty" value (eg a null)
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.

This article has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.