Hello I have a text file which contains a dictionery of phrases. I also have another text file. I need to see if any dictionery phrases are inside my larger text file and return the phrases which are in the dictionery. Here is my code so far:

private static void doCompare(CharSequence Key) throws FileNotFoundException, IOException {

String fileString = new String(Files.readAllBytes(Paths.get("dictonery/AB.txt")), StandardCharsets.UTF_8);


    Map<String, String> map = new HashMap<String, String>();
     String entireFileText = new Scanner(new File("dictonery/annotate.txt"))
    .useDelimiter("\\A").next();

    map.put(fileString, "Finish");

    for (String key : map.keySet()) {
        if(fileString.contains(key)) {

            BufferedWriter writer = null;
            try
            {
                writer = new BufferedWriter( new FileWriter( "annotate/AB.txt"));
                writer.write( key);

            }
            catch ( IOException e)
            {
            }
            finally
            {
                try
                {
                    if ( writer != null)
                    writer.close( );
                }
                catch ( IOException e)
                {
                }
        }
    }
     }



At the moment the whole dictionery is returned. How can I get it so just the terms that are matched in the dictionery are returned. Thanks

Rather than have a single String that contains the whole dictionary, you need to store and process each dictionary phrase separately. Eg if the phrases are on separate lines you could use Files.readAllLines to get a List of phrases.

and never do this:

catch ( IOException e)
            {
            }

I know, nobody likes to see error messages, but in your case, just because you don't see them, doesn't mean they're not there. you are merely hiding them.

at the very least, add a print statement informing you there is something wrong, with the message of the exception.

This article has been dead for over six months. Start a new discussion instead.