how can I read from txt file into AVL tree in java? error FileNotFoundException

111 views Asked by At

new java developer. this code isn't working. it keeps generating this error: java.io.FileNotFoundException: file1.txt (No such file or directory) even though the file is in the same folder (src) as the java classes

my program is supposed to count the word frequency of words in a txt file. I'm trying to read through the txt file, and put each word into a node of an AVL tree, along with the frequency of the word in the txt file so each node would look like this: ("word", 5). this is the code:

import java.io.BufferedReader;
import java.io.FileReader;
import java.util.Comparator;
import java.util.PriorityQueue;
import java.io.IOException;



public class FindKeyWordsInFile {
    
    //public AVLTree.Node root;
        
    public static void main(String[] args) {
        if (args.length != 3) {
            System.err.println("Usage: java FindKeyWordsInFile k file.txt MostFrequentEnglishWords.txt");
            System.exit(1);
        }

        int k = Integer.parseInt(args[0]);
        String inputFileName = args[1];
        String englishWordsFileName = args[2];
        
        AVLTree<String, Integer> wordFrequencies = new AVLTree<>();
        AVLTree<String, Void> englishWords = new AVLTree<>();
        AVLTree<String, Integer> keywordFrequencies = new AVLTree<>();
        
        try {       
            BufferedReader reader = new BufferedReader(new FileReader("file1.txt"));
            String line = reader.readLine();
            while (line != null) {
                String[] words = line.split("\\s+");
                for (String word : words) {
                    // Remove punctuation and convert to lower case
                    String cleanedWord = word.replaceAll("[^a-zA-Z0-9]", "").toLowerCase();
                    if (cleanedWord.length() > 0) {
                        Integer frequency = wordFrequencies.get(cleanedWord);
                        if (frequency == null) {
                            frequency = 0;
                        }
                        wordFrequencies.put(cleanedWord, frequency + 1);
                    }
                }
                line = reader.readLine();
            }
            reader.close();
0

There are 0 answers