As a newbie i m facing a small prob right now...

i have code to search names from many files and compare the results with similarity scoring.

its working but when the names found my program should show the filepath or fileName for each names.

how can i do this ??

Scanner scanner = new Scanner(System.in);
String SearchName;
System.out.print("Enter name to Search : ");
SearchName= scanner.next();

  for (String found: names) {
  double compare = SearchCompare.LCS(found, SearchName);
      if (compare !=null50.0) {
String f=found.toString();
    System.out.println("Found names : "+found+ "  Similarity score: " +compare+ "File Name: "+f);  }
    //else {System.out.println("No search result found"); }
    }

it should work with "f.getName()" but showing error...
pls help me..

Recommended Answers

All 4 Replies

String f=found.toString();

f is a String representation of found which is already a String. There is no reference to any files I can see.

here is full code now:

public class SearchCompare{ 

public static double LCS (String S, String T) {
S=S.toUpperCase();
T=T.toUpperCase();

if ((S.equals(""))||(T.equals(""))) 
return 0;
int[][] num = new int [S.length()] [T.length()];

int maxlen = 0;
for (int i = 0; i<S.length(); i++) {
    for (int j=0; j<T.length(); j++) {
if (S.charAt(i) !=T.charAt(j)) num [i][j] = 0;
else {
    if (( i==0) || (j==0)) 
    num [i][j]=1;
    else num [i][j] = 1 + num[i-1][j-1];
    if (num[i][j] >maxlen) {
    maxlen = num[i][j];
}
}
}

}
return (2.0*maxlen/(S.length()+T.length()))*100;
}

public static void main (String args[]){ 

FileReader reader;    BufferedReader br;    
String result="";     
String word= new String(); 
String target = "friend";
ArrayList<String> names = new ArrayList<String>();
try{ //read many files from test folder
File directory = new File("C:/Users/user/Desktop/java/test");

File file[]= directory.listFiles();

       for (File f: file) {
            
reader = new FileReader (f);        
br = new BufferedReader(reader);
Scanner scan = new Scanner(reader); 

while(scan.hasNext()){ scan.next();
result = scan.findWithinHorizon(target,0); 

if(result!=null) {
word = (scan.next() + scan.findWithinHorizon("", 0));


names.add(word);

for (String found: names){} 

}

  }
scan.close();
br.close(); 

}
    Scanner scanner = new Scanner(System.in);
String SearchName;
System.out.print("Enter name to Search : ");
SearchName= scanner.next();

  for (String found: names) {
  double compare = SearchCompare.LCS(found, SearchName);
      if (compare !=null) {
    System.out.println("Found names : "+found+ "  Similarity score: " +compare+ "File Name: "+found.toString());  }
    //else {System.out.println("No search result found"); }
    }
}
catch(Exception e){ System.err.println(e.getMessage()); 
}

}
  }

how can i get solution for this now... pls help me

you need to associate your names with the files they came from. currently you are only keeping track of the names and adding them to your arraylist.

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.