Home > Software design >  Why cant the system find the file that is specified?
Why cant the system find the file that is specified?

Time:11-07

in the assignment, the code is suppose to print on a single word from each file that repeats the most. I used a path to get to the list of files used for this assignment and i put them into an array. i cannot seems to find the problem as the array has all the files in it set to string. So why cant it find my file?

below is my code for single thread:

import java.io.*;
import java.util.*;



public class SingleThreaded  {


    public static void main(String[] args) throws IOException {
        File directoryPath = new File("C:\\assignment 3\\links");
        String[] dir = directoryPath.list();

        System.out.println(Arrays.toString(dir));
        
        String result;
        //Scanner scan;

        //try{
            //Scanner scan = new Scanner(System.in);

            long startTime = System.nanoTime();

            for (String file : dir) {
                //if(file.isFile()){

                    //BufferedReader inputStream = null;
                    String line;
                    
                    //int i;

                    try{
                        
                        //inputStream = new BufferedReader(new FileReader(file));file

                        Scanner scan = new Scanner(new File(file));

                        HashMap<String, Integer> map = new HashMap<>();

                        while (scan.hasNextLine()){
                            
                            line = scan.nextLine().replaceAll("\\p{Punct}", " ");
                            String[] word = line.split("\s ");
                                
                            for (int i = 0; i < word.length; i  ) {

                                String string = word[i].toLowerCase();

                                if (string.length() >= 5) {

                                    if (map.containsKey(string)) {

                                        map.put(string, map.get(string)   1);

                                    } else {

                                        map.put(string, 1);

                                    }
                                }
                            }
                        }
                        result = Collections.max(map.entrySet(), Comparator.comparingInt(Map.Entry::getValue)).getKey();
                        
                        System.out.println( file   ": "   result);

                    }catch (FileNotFoundException e) {

                        e.printStackTrace();
                    }
                   /* }finally{

                        if(inputStream != null){
                            inputStream.close();
                        }
                    }
                    */ 
               // }                           
            }
        long endTime = System.nanoTime();
        long totalTime = (endTime - startTime)/1000;     
        System.out.print("Total Time: "   totalTime);
        
        //} catch (FileNotFoundException e) {

            //e.printStackTrace();
        //}
    }
}    

I tried changing the path and using different built-in methods but nothing seems to work.

CodePudding user response:

Try this:

public static void main(String[] args) throws IOException {

  File directoryPath = new File("C:\\assignment 3\\links");
  File[] files = directoryPath.listFiles();

  for (File file : files) {

   Scanner scan = new Scanner(file);

Notice the difference, using File.listFiles() instead of File.list().

  • Related