As a java beginnners what i incline to do for my program almost get solution from this site.

Right now i need a small help to get file name or full path for output files of my program. so far my program is running as a search engine to find all of my friends name from many files and compare the similarity with a constructor "LCS".

the part of my code:

FileReader reader; BufferedReader br;
String result=""; String word= new String();
String target = "friend";
ArrayList names = new ArrayList();

try{ //read many files from folder
File directory = new File("C:/Users/Test");

File file[]= directory.listFiles();

for (File allFiles: file) {

reader = new FileReader (allFiles);
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 = ReadFilesAndCompare.LCS(found, searchName);
//if (compare >= 50.0) {
System.out.println("Found names : "+found+ "\t" +"Similarity score: " +compare); }

what should i use here to getFile name for my found result ??

Can you please give a easiest solution for me ?

i want to make output like this :

Found names: ..... Similarity score: ..... File name: ....

right now just need to get file path or file name for my found names but i cant make it.
Anybody can please help me ...

Unclosed try{.
You have all, what you need.

void test0() throws FileNotFoundException, IOException {
        FileReader reader;
        BufferedReader br;
        String result = "";
        String word = new String();
        String target = "friend";
        ArrayList<String> names = new ArrayList<String>();//1.
        // try { //read many files from folder
        File directory = new File("C:/Test");
        File file[] = directory.listFiles();
        for (File allFiles : file) {
            System.out.println("fileName " + allFiles);//2.
            reader = new FileReader(allFiles);
            br = new BufferedReader(reader);
            Scanner scan = new Scanner(reader);
            while (scan.hasNext()) {
                scan.next();
                result = scan.findWithinHorizon(target, 0);
                if (result != null) {
                    System.out.println("*\t" + result);//3.
                    word = (scan.next() + scan.findWithinHorizon("", 0));
                    System.out.println("**\t" + word);//4.
                    names.add(word);
                    for (String found : names) {
                        System.out.println("***\t" + found);//5.
                    }
                }
            }
            scan.close();
            br.close();
        }
    //....
    }

Properly write try-catch in place: throws FileNotFoundException, IOException {

thanks for ur reply..

here i given a part of my code so somehow it missed unclose '{ / }' but in my program i have all these things that u mentioned.

Actually my code does the following...

> Go through every file in a directory
> Extract names from the files and store it in a list
> Take an input name from the user
> For each name in the extracted list apply some algorithm to
calculate a score against the user input name and print the results.

And when printing the results, i need the name of the file where the name was originally found.

according to my 1st post the program is running correctly without having the fileName(), right now i just need it only.

please help me.............

You need to expand the program to store fileName and friendList.
In new class. For example Result.

class Result {

        private String fileName;
        private List<String> friendNames;

        Result(String fileName) {
            this.fileName = fileName;
            friendNames = new ArrayList<String>();
        }

        Result(File file) {
            this.fileName = file.getName();
            friendNames = new ArrayList<String>();
        }

        Result(String fileName, List<String> friendNames) {
            this.fileName = fileName;
            this.friendNames = friendNames;
        }

        void addFriendName(String friendName) {
            friendNames.add(friendName);
        }

        public String toString() {
            return "fileName " + fileName + friendNames;
        }
        //...TODO
    }

Store instances of Result in list:

List<Result> results = new ArrayList<Result>();

thanks a lot for ur kind help.

its working now.................

This question has already been answered. Start a new discussion instead.