Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

can someone please help me with this code, i can`t get it to run. import java.ut

ID: 3677923 • Letter: C

Question

can someone please help me with this code, i can`t get it to run.

import java.util.*;

import java.io.*;

class EditDistance
{

public static void main(String[]args)throws IOException
{  Scanner s = new Scanner(System.in);
  int i = 0, n1, n2;
  int edit_distance = 0;
  boolean flag1 = false, flag2 = false, flag[];

  StringBuffer word1;
  StringBuffer word2;
  String t;
  
FileReader fr = new FileReader("src/Words.txt");
  BufferedReader br = new BufferedReader(fr);
  
System.out.print("Enter word:");
  word1 = new StringBuffer(s.next());
  flag = new boolean [word1.length()];

System.out.print("Enter Destination word:");
  word2 = new StringBuffer(s.next());
  
  while ((t = br.readLine())!=null)
  {
   if(t.equals(word1.toString()))
    flag1 = true;
   if(t.equals(word2.toString()))
    flag2 = true;
   if((flag1 == false)||(flag2 == false))
   
   {
System.out.println("Invalid word(s).!");
   }
   
   else if(word1.length()!= word2.length())
    System.out.println("Both words are" + "not of same lenght");
   
   else
   {
    System.out.println("Intermediate" + "Transition Words" + word1.toString());
   
  i = 0;
  int j = 0, k = 0;
  int already_occured;
  while(i < word1.length())
  {
   if(word1.charAt(i)==word2.charAt(i))
   {
    flag[i]=true;
   
   if(flag[k]==false)
   {
    already_occured = 1;
    i = k - 1;
    i++;
   break;
   }
   }
    continue;
  }
  else;
  {
   char temp1 = word1.charAt(i);
   char temp2 = word2.charAt(i);
   
    fr = new FileReader("src/Words.txt");
    br = new BufferedReader(fr);
    
  while((t = br.readLine())!=null)
  {
   if(t.equals(word1.toString()))
   {
    System.out.println(word1.toString());
    flag[i] = true;
    
  break;
   }
  }
   if(flag[i]== false)
   {
    word1.setCharAt(i, temp1);
    char c = 'a';
      
      fr = new FileReader("words.txt");
      br = new BufferedReader(fr);
   
      for(j=0;j<26;j++,c++)
   {
   if (c == temp1)
    continue;
   while((t = br.readLine())!= null)
   {
    System.out.println(word1.toString());
    flag[i] = true;
    
   break;
   }
   }
    
   if(flag[i]== true)
   break;
   else
    word1.setCharAt(i, temp1);
   }
    fr.close();
  }
   if(i== word1.length() - 1)
    for(k = 0; k < word1.length(); k++)
    {
   if(already_occured!=0)
    break;
   
   else if(flag[k]== false)
   {
    already_occured =1;
    i = k - 1;
   
    break;
   }
    }
   i++;
   }
   if(!word1.toString().equals(word2.toString()))
    System.out.println("To possible match....");
   else
    System.out.println("Edit Distance=" + edit_distance);
  }
   }

Explanation / Answer

Every value in java must be initialized before using hence in your programe variable already_counted must be initialized first.

You may think that you have initialized already_counted. but that was done in if condition. What if if condition won't be executed. That is why java describes it as a compiler error.

And there is another error at else condition. I think else is used out of blocks means without Ifs.

Thank you.

Hope you can get the solution now.

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote